From 5913552dfec4f6e1a812a558cb54b3ca386de1f3 Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Mon, 13 Nov 2017 19:57:11 -0500 Subject: [PATCH] Fix style issues in stylesheet tests --- tests/helpers/fixtures.py | 4 +- tests/helpers/utils.py | 3 +- tests/unit/javascript/conftest.py | 76 ++++++++++--------- .../position_caret/test_position_caret.py | 4 +- .../javascript/stylesheet/test_stylesheet.py | 15 ++-- 5 files changed, 55 insertions(+), 47 deletions(-) diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py index 8caf6fcf5..6e17ebd71 100644 --- a/tests/helpers/fixtures.py +++ b/tests/helpers/fixtures.py @@ -37,7 +37,7 @@ import pytest import py.path # pylint: disable=no-name-in-module import helpers.stubs as stubsmod -from helpers.utils import CallbackChecker +import helpers.utils from qutebrowser.config import config, configdata, configtypes, configexc from qutebrowser.utils import objreg, standarddir from qutebrowser.browser.webkit import cookies @@ -81,7 +81,7 @@ class WinRegistryHelper: @pytest.fixture def callback_checker(qtbot): - return CallbackChecker(qtbot) + return helpers.utils.CallbackChecker(qtbot) class FakeStatusBar(QWidget): diff --git a/tests/helpers/utils.py b/tests/helpers/utils.py index 45141f7dc..82c07fbd2 100644 --- a/tests/helpers/utils.py +++ b/tests/helpers/utils.py @@ -27,9 +27,10 @@ import contextlib import pytest -from qutebrowser.utils import qtutils from PyQt5.QtCore import QObject, pyqtSignal +from qutebrowser.utils import qtutils + qt58 = pytest.mark.skipif( qtutils.version_check('5.9'), reason="Needs Qt 5.8 or earlier") diff --git a/tests/unit/javascript/conftest.py b/tests/unit/javascript/conftest.py index bcfc6b47a..7290d4d00 100644 --- a/tests/unit/javascript/conftest.py +++ b/tests/unit/javascript/conftest.py @@ -25,9 +25,12 @@ import logging import pytest import jinja2 -from helpers.utils import CallbackChecker + 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: from PyQt5.QtWebKit import QWebSettings @@ -46,8 +49,6 @@ except ImportError: QWebEngineSettings = None QWebEngineScript = None -from qutebrowser.utils import utils - if QWebPage is None: TestWebPage = None @@ -110,10 +111,12 @@ else: def javaScriptConsoleMessage(self, level, msg, line, source): """Fail tests on js console messages as they're used for errors.""" pytest.fail("[{}] js console ({}:{}): {}".format( - qenum_key(QWebEnginePage, level), source, line, msg)) + qutebrowser.utils.debug.qenum_key( + QWebEnginePage, level), source, line, msg)) class JSTester: + """Common subclass providing basic functionality for all JS testers. Attributes: @@ -141,6 +144,28 @@ class JSTester: self.webview.setHtml(template.render(**kwargs)) 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): @@ -153,7 +178,7 @@ class JSWebKitTester(JSTester): """ def __init__(self, webview, qtbot): - JSTester.__init__(self, webview, qtbot) + super().__init__(webview, qtbot) self.webview.setPage(TestWebPage(self.webview)) def scroll_anchor(self, name): @@ -201,32 +226,9 @@ class JSWebEngineTester(JSTester): _jinja_env: The jinja2 environment used to get templates. """ - def __init__(self, webview, callback_checker, qtbot): - JSTester.__init__(self, webview, qtbot) + def __init__(self, webview, qtbot): + super().__init__(webview, qtbot) 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: """Run a javascript file. @@ -239,8 +241,7 @@ class JSWebEngineTester(JSTester): source = utils.read_file(os.path.join('javascript', filename)) self.run(source, expected) - def run(self, source: str, expected, - world=QWebEngineScript.ApplicationWorld) -> None: + def run(self, source: str, expected, world=None) -> None: """Run the given javascript source. Args: @@ -248,7 +249,10 @@ class JSWebEngineTester(JSTester): expected: The value expected return from the javascript execution 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( QWebEngineSettings.JavascriptEnabled) self.webview.page().runJavaScript(source, world, @@ -257,7 +261,7 @@ class JSWebEngineTester(JSTester): @pytest.fixture -def js_tester(webview, qtbot): +def js_tester_webkit(webview, qtbot): """Fixture to test javascript snippets in webkit.""" return JSWebKitTester(webview, qtbot) @@ -265,4 +269,4 @@ def js_tester(webview, qtbot): @pytest.fixture def js_tester_webengine(callback_checker, webengineview, qtbot): """Fixture to test javascript snippets in webengine.""" - return JSWebEngineTester(webengineview, callback_checker, qtbot) + return JSWebEngineTester(webengineview, qtbot) diff --git a/tests/unit/javascript/position_caret/test_position_caret.py b/tests/unit/javascript/position_caret/test_position_caret.py index 7be62e3cc..fcfa5cf5d 100644 --- a/tests/unit/javascript/position_caret/test_position_caret.py +++ b/tests/unit/javascript/position_caret/test_position_caret.py @@ -65,9 +65,9 @@ class CaretTester: @pytest.fixture -def caret_tester(js_tester): +def caret_tester(js_tester_webkit): """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 # succeed in some cases, see #1988 caret_tester.js.webview.show() diff --git a/tests/unit/javascript/stylesheet/test_stylesheet.py b/tests/unit/javascript/stylesheet/test_stylesheet.py index 179240d42..f74c04b8e 100644 --- a/tests/unit/javascript/stylesheet/test_stylesheet.py +++ b/tests/unit/javascript/stylesheet/test_stylesheet.py @@ -21,8 +21,10 @@ import os import pytest -from qutebrowser.utils import javascript + from PyQt5.QtWebEngineWidgets import QWebEngineProfile + +from qutebrowser.utils import javascript from qutebrowser.browser.webengine import webenginesettings @@ -61,12 +63,13 @@ class StylesheetTester: document_element="document.body"): """Check whether the css in ELEMENT is set to VALUE.""" self.js.run("window.getComputedStyle({}, null)" - ".getPropertyValue('{}');".format(document_element, - css_style), value) + ".getPropertyValue('{}');" + .format(document_element, + javascript.string_escape(css_style)), value) def check_eq(self, one, two, true=True): """Check if one and two are equal.""" - self.js.run("{} === {}".format(one, two), true) + self.js.run("{} === {};".format(one, two), true) @pytest.fixture @@ -103,7 +106,7 @@ def test_set_xml(stylesheet_tester): stylesheet_tester.init_stylesheet() stylesheet_tester.js.load_file('stylesheet/simple.xml') 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): @@ -112,7 +115,7 @@ def test_set_svg(stylesheet_tester): stylesheet_tester.js.load_file('../../../misc/cheatsheet.svg') stylesheet_tester.check_set(GREEN_BODY_BG, 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):