Fix style issues in stylesheet tests

This commit is contained in:
Jay Kamat 2017-11-13 19:57:11 -05:00
parent 324c537a3d
commit 5913552dfe
No known key found for this signature in database
GPG Key ID: 5D2E399600F4F7B5
5 changed files with 55 additions and 47 deletions

View File

@ -37,7 +37,7 @@ 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 import helpers.utils
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
@ -81,7 +81,7 @@ class WinRegistryHelper:
@pytest.fixture @pytest.fixture
def callback_checker(qtbot): def callback_checker(qtbot):
return CallbackChecker(qtbot) return helpers.utils.CallbackChecker(qtbot)
class FakeStatusBar(QWidget): class FakeStatusBar(QWidget):

View File

@ -27,9 +27,10 @@ import contextlib
import pytest import pytest
from qutebrowser.utils import qtutils
from PyQt5.QtCore import QObject, pyqtSignal from PyQt5.QtCore import QObject, pyqtSignal
from qutebrowser.utils import qtutils
qt58 = pytest.mark.skipif( qt58 = pytest.mark.skipif(
qtutils.version_check('5.9'), reason="Needs Qt 5.8 or earlier") qtutils.version_check('5.9'), reason="Needs Qt 5.8 or earlier")

View File

@ -25,9 +25,12 @@ import logging
import pytest import pytest
import jinja2 import jinja2
from helpers.utils import CallbackChecker
from PyQt5.QtCore import QUrl from PyQt5.QtCore import QUrl
from qutebrowser.utils.debug import qenum_key
import helpers.utils
import qutebrowser.utils.debug
from qutebrowser.utils import utils
try: try:
from PyQt5.QtWebKit import QWebSettings from PyQt5.QtWebKit import QWebSettings
@ -46,8 +49,6 @@ except ImportError:
QWebEngineSettings = None QWebEngineSettings = None
QWebEngineScript = None QWebEngineScript = None
from qutebrowser.utils import utils
if QWebPage is None: if QWebPage is None:
TestWebPage = None TestWebPage = None
@ -110,10 +111,12 @@ else:
def javaScriptConsoleMessage(self, level, msg, line, source): def javaScriptConsoleMessage(self, level, msg, line, source):
"""Fail tests on js console messages as they're used for errors.""" """Fail tests on js console messages as they're used for errors."""
pytest.fail("[{}] js console ({}:{}): {}".format( pytest.fail("[{}] js console ({}:{}): {}".format(
qenum_key(QWebEnginePage, level), source, line, msg)) qutebrowser.utils.debug.qenum_key(
QWebEnginePage, level), source, line, msg))
class JSTester: class JSTester:
"""Common subclass providing basic functionality for all JS testers. """Common subclass providing basic functionality for all JS testers.
Attributes: Attributes:
@ -141,6 +144,28 @@ class JSTester:
self.webview.setHtml(template.render(**kwargs)) self.webview.setHtml(template.render(**kwargs))
assert blocker.args == [True] assert blocker.args == [True]
def load_file(self, path: str, force: bool = False):
"""Load a file from disk.
Args:
path: The string path from disk to load (relative to this file)
force: Whether to force loading even if the file is invalid.
"""
self.load_url(QUrl.fromLocalFile(
os.path.join(os.path.dirname(__file__), path)), force)
def load_url(self, url: QUrl, force: bool = False):
"""Load a given QUrl.
Args:
url: The QUrl to load.
force: Whether to force loading even if the file is invalid.
"""
with self._qtbot.waitSignal(self.webview.loadFinished) as blocker:
self.webview.load(url)
if not force:
assert blocker.args == [True]
class JSWebKitTester(JSTester): class JSWebKitTester(JSTester):
@ -153,7 +178,7 @@ class JSWebKitTester(JSTester):
""" """
def __init__(self, webview, qtbot): def __init__(self, webview, qtbot):
JSTester.__init__(self, webview, qtbot) super().__init__(webview, qtbot)
self.webview.setPage(TestWebPage(self.webview)) self.webview.setPage(TestWebPage(self.webview))
def scroll_anchor(self, name): def scroll_anchor(self, name):
@ -201,32 +226,9 @@ class JSWebEngineTester(JSTester):
_jinja_env: The jinja2 environment used to get templates. _jinja_env: The jinja2 environment used to get templates.
""" """
def __init__(self, webview, callback_checker, qtbot): def __init__(self, webview, qtbot):
JSTester.__init__(self, webview, qtbot) super().__init__(webview, qtbot)
self.webview.setPage(TestWebEnginePage(self.webview)) self.webview.setPage(TestWebEnginePage(self.webview))
self.callback_checker = callback_checker
def load_file(self, path: str, force=False):
"""Load a file from disk.
Args:
path: The string path from disk to load (relative to this file)
force: Whether to force loading even if the file is invalid.
"""
self.load_url(QUrl.fromLocalFile(
os.path.join(os.path.dirname(__file__), path)), force)
def load_url(self, url: QUrl, force: bool = False):
"""Load a given QUrl.
Args:
url: The QUrl to load.
force: Whether to force loading even if the file is invalid.
"""
with self._qtbot.waitSignal(self.webview.loadFinished) as blocker:
self.webview.load(url)
if not force:
assert blocker.args == [True]
def run_file(self, filename: str, expected) -> None: def run_file(self, filename: str, expected) -> None:
"""Run a javascript file. """Run a javascript file.
@ -239,8 +241,7 @@ class JSWebEngineTester(JSTester):
source = utils.read_file(os.path.join('javascript', filename)) source = utils.read_file(os.path.join('javascript', filename))
self.run(source, expected) self.run(source, expected)
def run(self, source: str, expected, def run(self, source: str, expected, world=None) -> None:
world=QWebEngineScript.ApplicationWorld) -> None:
"""Run the given javascript source. """Run the given javascript source.
Args: Args:
@ -248,7 +249,10 @@ class JSWebEngineTester(JSTester):
expected: The value expected return from the javascript execution expected: The value expected return from the javascript execution
world: The scope the javascript will run in world: The scope the javascript will run in
""" """
callback_checker = CallbackChecker(self._qtbot) if world is None:
world = QWebEngineScript.ApplicationWorld
callback_checker = helpers.utils.CallbackChecker(self._qtbot)
assert self.webview.settings().testAttribute( assert self.webview.settings().testAttribute(
QWebEngineSettings.JavascriptEnabled) QWebEngineSettings.JavascriptEnabled)
self.webview.page().runJavaScript(source, world, self.webview.page().runJavaScript(source, world,
@ -257,7 +261,7 @@ class JSWebEngineTester(JSTester):
@pytest.fixture @pytest.fixture
def js_tester(webview, qtbot): def js_tester_webkit(webview, qtbot):
"""Fixture to test javascript snippets in webkit.""" """Fixture to test javascript snippets in webkit."""
return JSWebKitTester(webview, qtbot) return JSWebKitTester(webview, qtbot)
@ -265,4 +269,4 @@ def js_tester(webview, qtbot):
@pytest.fixture @pytest.fixture
def js_tester_webengine(callback_checker, webengineview, qtbot): def js_tester_webengine(callback_checker, webengineview, qtbot):
"""Fixture to test javascript snippets in webengine.""" """Fixture to test javascript snippets in webengine."""
return JSWebEngineTester(webengineview, callback_checker, qtbot) return JSWebEngineTester(webengineview, qtbot)

View File

@ -65,9 +65,9 @@ class CaretTester:
@pytest.fixture @pytest.fixture
def caret_tester(js_tester): def caret_tester(js_tester_webkit):
"""Helper fixture to test caret browsing positions.""" """Helper fixture to test caret browsing positions."""
caret_tester = CaretTester(js_tester) caret_tester = CaretTester(js_tester_webkit)
# Showing webview here is necessary for test_scrolled_down_img to # Showing webview here is necessary for test_scrolled_down_img to
# succeed in some cases, see #1988 # succeed in some cases, see #1988
caret_tester.js.webview.show() caret_tester.js.webview.show()

View File

@ -21,8 +21,10 @@
import os import os
import pytest import pytest
from qutebrowser.utils import javascript
from PyQt5.QtWebEngineWidgets import QWebEngineProfile from PyQt5.QtWebEngineWidgets import QWebEngineProfile
from qutebrowser.utils import javascript
from qutebrowser.browser.webengine import webenginesettings from qutebrowser.browser.webengine import webenginesettings
@ -61,12 +63,13 @@ class StylesheetTester:
document_element="document.body"): document_element="document.body"):
"""Check whether the css in ELEMENT is set to VALUE.""" """Check whether the css in ELEMENT is set to VALUE."""
self.js.run("window.getComputedStyle({}, null)" self.js.run("window.getComputedStyle({}, null)"
".getPropertyValue('{}');".format(document_element, ".getPropertyValue('{}');"
css_style), value) .format(document_element,
javascript.string_escape(css_style)), value)
def check_eq(self, one, two, true=True): def check_eq(self, one, two, true=True):
"""Check if one and two are equal.""" """Check if one and two are equal."""
self.js.run("{} === {}".format(one, two), true) self.js.run("{} === {};".format(one, two), true)
@pytest.fixture @pytest.fixture
@ -103,7 +106,7 @@ def test_set_xml(stylesheet_tester):
stylesheet_tester.init_stylesheet() stylesheet_tester.init_stylesheet()
stylesheet_tester.js.load_file('stylesheet/simple.xml') stylesheet_tester.js.load_file('stylesheet/simple.xml')
stylesheet_tester.check_set(GREEN_BODY_BG) stylesheet_tester.check_set(GREEN_BODY_BG)
stylesheet_tester.check_eq("\"html\"", "document.documentElement.nodeName") stylesheet_tester.check_eq('"html"', "document.documentElement.nodeName")
def test_set_svg(stylesheet_tester): def test_set_svg(stylesheet_tester):
@ -112,7 +115,7 @@ def test_set_svg(stylesheet_tester):
stylesheet_tester.js.load_file('../../../misc/cheatsheet.svg') stylesheet_tester.js.load_file('../../../misc/cheatsheet.svg')
stylesheet_tester.check_set(GREEN_BODY_BG, stylesheet_tester.check_set(GREEN_BODY_BG,
document_element="document.documentElement") document_element="document.documentElement")
stylesheet_tester.check_eq("\"svg\"", "document.documentElement.nodeName") stylesheet_tester.check_eq('"svg"', "document.documentElement.nodeName")
def test_set_error(stylesheet_tester): def test_set_error(stylesheet_tester):