From 03eae9140e4cfa94a884145d26fafd26dd71d60c Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Wed, 8 Nov 2017 22:58:35 -0500 Subject: [PATCH] Implement proper loading of stylesheet.js --- tests/unit/javascript/conftest.py | 24 +++++-- tests/unit/javascript/stylesheet/green.css | 1 + tests/unit/javascript/stylesheet/none.css | 0 tests/unit/javascript/stylesheet/simple.xml | 43 ++++++++++++ .../javascript/stylesheet/test_stylesheet.py | 65 ++++++++++--------- 5 files changed, 99 insertions(+), 34 deletions(-) create mode 100644 tests/unit/javascript/stylesheet/green.css create mode 100644 tests/unit/javascript/stylesheet/none.css create mode 100644 tests/unit/javascript/stylesheet/simple.xml diff --git a/tests/unit/javascript/conftest.py b/tests/unit/javascript/conftest.py index 788daf568..25fc9ea9d 100644 --- a/tests/unit/javascript/conftest.py +++ b/tests/unit/javascript/conftest.py @@ -28,9 +28,10 @@ import jinja2 from tests.helpers.fixtures import CallbackChecker try: + from PyQt5.QtCore import QUrl, QFile, QFileInfo from PyQt5.QtWebKit import QWebSettings from PyQt5.QtWebKitWidgets import QWebPage - from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineSettings + from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineSettings, QWebEngineScript except ImportError: # FIXME:qtwebengine Make these tests use the tab API QWebSettings = None @@ -194,6 +195,19 @@ class JSWebEngineTester: self.webview.setHtml(template.render(**kwargs)) assert blocker.args == [True] + def load_file(self, path: str): + """Loads a file from disk""" + self.load_url(QUrl.fromLocalFile( + os.path.join(os.path.dirname(__file__), path))) + + def load_url(self, url: QUrl): + """Load a given QUrl.""" + with self._qtbot.waitSignal(self.webview.loadFinished) as blocker: + self.webview.load(url) + assert blocker.args == [True] + import time + time.sleep(1) + def run_file(self, filename, expected): """Run a javascript file. @@ -207,7 +221,7 @@ class JSWebEngineTester: source = utils.read_file(os.path.join('javascript', filename)) self.run(source, expected) - def run(self, source, expected): + def run(self, source, expected, world=QWebEngineScript.ApplicationWorld): """Run the given javascript source. Args: @@ -218,8 +232,10 @@ class JSWebEngineTester: """ # TODO how to do this properly callback_checker = CallbackChecker(self._qtbot) - assert self.webview.settings().testAttribute(QWebEngineSettings.JavascriptEnabled) - self.webview.page().runJavaScript(source, callback_checker.callback) + assert self.webview.settings().testAttribute( + QWebEngineSettings.JavascriptEnabled) + self.webview.page().runJavaScript(source, world, + callback_checker.callback) callback_checker.check(expected) diff --git a/tests/unit/javascript/stylesheet/green.css b/tests/unit/javascript/stylesheet/green.css new file mode 100644 index 000000000..35832971a --- /dev/null +++ b/tests/unit/javascript/stylesheet/green.css @@ -0,0 +1 @@ +body {background-color: rgb(0, 255, 0);} diff --git a/tests/unit/javascript/stylesheet/none.css b/tests/unit/javascript/stylesheet/none.css new file mode 100644 index 000000000..e69de29bb diff --git a/tests/unit/javascript/stylesheet/simple.xml b/tests/unit/javascript/stylesheet/simple.xml new file mode 100644 index 000000000..bac4539ab --- /dev/null +++ b/tests/unit/javascript/stylesheet/simple.xml @@ -0,0 +1,43 @@ + + + + org.qutebrowser.qutebrowser + CC-BY-SA-3.0 + GPL-3.0 + qutebrowser + A keyboard-driven web browser + +

+ qutebrowser is a keyboard-focused browser with a minimal GUI. + It was inspired by other browsers/addons like dwb and Vimperator/Pentadactyl, + and is based on Python and PyQt5. +

+
+ + Network + WebBrowser + + + qutebrowser + + qutebrowser.desktop + + + https://raw.githubusercontent.com/qutebrowser/qutebrowser/master/doc/img/main.png + + + https://raw.githubusercontent.com/qutebrowser/qutebrowser/master/doc/img/downloads.png + + + https://raw.githubusercontent.com/qutebrowser/qutebrowser/master/doc/img/completion.png + + + https://raw.githubusercontent.com/qutebrowser/qutebrowser/master/doc/img/hints.png + + + https://www.qutebrowser.org + https://qutebrowser.org/doc/faq.html + https://qutebrowser.org/doc/help/ + https://github.com/qutebrowser/qutebrowser/issues/ + https://github.com/qutebrowser/qutebrowser#donating +
diff --git a/tests/unit/javascript/stylesheet/test_stylesheet.py b/tests/unit/javascript/stylesheet/test_stylesheet.py index 9057f249b..ee557b2a1 100644 --- a/tests/unit/javascript/stylesheet/test_stylesheet.py +++ b/tests/unit/javascript/stylesheet/test_stylesheet.py @@ -21,12 +21,16 @@ import os import pytest -from qutebrowser.utils import javascript +from qutebrowser.utils import javascript, utils from qutebrowser.browser import shared from qutebrowser.config import config -from PyQt5.QtWebEngineWidgets import QWebEngineSettings +from PyQt5.QtWebEngineWidgets import QWebEngineSettings, QWebEngineProfile, QWebEngineScript +import qutebrowser.browser.webengine.webenginesettings as webenginesettings DEFAULT_BODY_BG = "rgba(0, 0, 0, 0)" +GREEN_BODY_BG = "rgb(0, 255, 0)" +CSS_BODY_GREEN = "body {background-color: rgb(0, 255, 0);}" +CSS_BODY_RED = "body {background-color: rgb(255, 0, 0);}" class StylesheetTester: @@ -36,60 +40,61 @@ class StylesheetTester: js: The js_tester fixture. """ - def __init__(self, js_tester): + def __init__(self, js_tester, config_stub): self.js = js_tester + self.config_stub = config_stub - def init_stylesheet(self): - """Initializes stylesheet. - Run after document is loaded.""" - self.js.run('window._qutebrowser = window._qutebrowser || {};', {}) - self.js.run_file('stylesheet.js', {}) + def init_stylesheet(self, css_file="green.css"): + + self.config_stub.val.content.user_stylesheets = \ + os.path.join(os.path.dirname(__file__), css_file) + p = QWebEngineProfile.defaultProfile() + webenginesettings._init_stylesheet(p) def set_css(self, css): """Set css to CSS via stylesheet.js.""" code = javascript.assemble('stylesheet', 'set_css', css) self.js.run(code, None) - def check_set(self, element, value): + def check_set(self, value, element="background-color"): """Check whether the css in ELEMENT is set to VALUE.""" self.js.run("window.getComputedStyle(document.body, null)" ".getPropertyValue('{}');".format(element), value) @pytest.fixture -def stylesheet_tester(js_tester_webengine): +def stylesheet_tester(js_tester_webengine, config_stub): """Helper fixture to test caret browsing positions.""" - ss_tester = StylesheetTester(js_tester_webengine) + ss_tester = StylesheetTester(js_tester_webengine, config_stub) # Showing webview here is necessary for test_scrolled_down_img to # succeed in some cases, see #1988 ss_tester.js.webview.show() return ss_tester -@pytest.mark.parametrize('init', [False, True]) -@pytest.mark.parametrize('page,expected', [('stylesheet/simple.html', DEFAULT_BODY_BG), - ('stylesheet/simple_bg_set_red.html', "rgb(255, 0, 0)")]) -def test_no_set_stylesheet(stylesheet_tester, init, page, expected): - stylesheet_tester.js.load(page) - if init: - stylesheet_tester.init_stylesheet() - stylesheet_tester.check_set("background-color", expected) - @pytest.mark.parametrize('page', ['stylesheet/simple.html', 'stylesheet/simple_bg_set_red.html']) @pytest.mark.parametrize('set_js', [True, False]) -def test_simple_set_bg(stylesheet_tester, page, set_js): +def test_set_delayed(stylesheet_tester, page, set_js): + stylesheet_tester.init_stylesheet("none.css") stylesheet_tester.js.load(page) if set_js: - stylesheet_tester.js.run('document.body.style.backgroundColor = "red";', 'red') + stylesheet_tester.js.run( + 'document.body.style.backgroundColor = "red";', 'red') pytest.xfail("overring values set with js does not work.") - stylesheet_tester.init_stylesheet() - stylesheet_tester.set_css("body {background-color: rgb(10, 10, 10);}") - stylesheet_tester.check_set("background-color", "rgb(10, 10, 10)") + stylesheet_tester.set_css("body {background-color: rgb(0, 255, 0);}") + stylesheet_tester.check_set("rgb(0, 255, 0)") -def test_simple_set_clear_bg(stylesheet_tester): - stylesheet_tester.js.load('stylesheet/simple.html') +@pytest.mark.parametrize('page', ['stylesheet/simple.html', + 'stylesheet/simple_bg_set_red.html']) +def test_set_clear_bg(stylesheet_tester, page): stylesheet_tester.init_stylesheet() - stylesheet_tester.set_css("body {background-color: rgb(10, 10, 10);}") - stylesheet_tester.check_set("background-color", "rgb(10, 10, 10)") + stylesheet_tester.js.load('stylesheet/simple.html') + stylesheet_tester.check_set(GREEN_BODY_BG) stylesheet_tester.set_css("") - stylesheet_tester.check_set("background-color", DEFAULT_BODY_BG) + stylesheet_tester.check_set(DEFAULT_BODY_BG) + +def test_no_set_xml(stylesheet_tester): + stylesheet_tester.init_stylesheet() + pytest.xfail("loading xml files throws exceptions") + stylesheet_tester.js.load_file('stylesheet/simple.xml') + stylesheet_tester.check_set(DEFAULT_BODY_BG)