Clean up stylesheet tests

This commit is contained in:
Jay Kamat 2017-11-09 00:07:54 -05:00
parent 2f9a857a27
commit 5ac8e5ad3e
No known key found for this signature in database
GPG Key ID: 5D2E399600F4F7B5
4 changed files with 71 additions and 88 deletions

View File

@ -26,17 +26,25 @@ import logging
import pytest
import jinja2
from tests.helpers.fixtures import CallbackChecker
from PyQt5.QtCore import QUrl
try:
from PyQt5.QtCore import QUrl, QFile, QFileInfo
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtWebKitWidgets import QWebPage
from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineSettings, QWebEngineScript
except ImportError:
# FIXME:qtwebengine Make these tests use the tab API
QWebSettings = None
QWebPage = None
try:
from PyQt5.QtWebEngineWidgets import (QWebEnginePage,
QWebEngineSettings,
QWebEngineScript)
except ImportError:
QWebEnginePage = None
QWebEngineSettings = None
QWebEngineScript = None
from qutebrowser.utils import utils
@ -71,9 +79,12 @@ else:
"""Fail tests on js console messages as they're used for errors."""
pytest.fail("js console ({}:{}): {}".format(source, line, msg))
if QWebEnginePage is None:
TestWebEnginePage = None
else:
class TestWebEnginePage(QWebEnginePage):
"""QWebPage subclass which overrides some test methods.
"""QWebEnginePage which overrides javascript logging methods.
Attributes:
_logger: The logger used for alerts.
@ -97,7 +108,8 @@ 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(level, source, line, msg))
pytest.fail("[{}] js console ({}:{}): {}".format(level, source,
line, msg))
class JSTester:
@ -164,9 +176,10 @@ class JSTester:
QWebSettings.JavascriptEnabled)
return self.webview.page().mainFrame().evaluateJavaScript(source)
class JSWebEngineTester:
"""Object returned by js_tester_webengine which provides test data and a webview.
"""Object returned by js_tester_webengine which provides a webview.
Attributes:
webview: The webview which is used.
@ -183,7 +196,7 @@ class JSWebEngineTester:
self._jinja_env = jinja2.Environment(loader=loader, autoescape=True)
def load(self, path, **kwargs):
"""Load and display the given test data.
"""Load and display the given jinja test data.
Args:
path: The path to the test file, relative to the javascript/
@ -196,42 +209,47 @@ class JSWebEngineTester:
assert blocker.args == [True]
def load_file(self, path: str, force=False):
"""Loads a file from disk"""
"""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=False):
"""Load a given QUrl."""
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]
import time
time.sleep(1)
def run_file(self, filename, expected):
def run_file(self, filename: str, expected) -> None:
"""Run a javascript file.
Args:
filename: The javascript filename, relative to
qutebrowser/javascript.
Return:
The javascript return value.
expected: The value expected return from the javascript execution
"""
source = utils.read_file(os.path.join('javascript', filename))
self.run(source, expected)
def run(self, source, expected, world=QWebEngineScript.ApplicationWorld):
def run(self, source: str, expected,
world=QWebEngineScript.ApplicationWorld) -> None:
"""Run the given javascript source.
Args:
source: The source to run as a string.
Return:
The javascript return value.
expected: The value expected return from the javascript execution
world: The scope the javascript will run in
"""
# TODO how to do this properly
callback_checker = CallbackChecker(self._qtbot)
assert self.webview.settings().testAttribute(
QWebEngineSettings.JavascriptEnabled)
@ -242,13 +260,11 @@ class JSWebEngineTester:
@pytest.fixture
def js_tester(webview, qtbot):
"""Fixture to test javascript snippets."""
"""Fixture to test javascript snippets in webkit."""
return JSTester(webview, qtbot)
@pytest.fixture
def js_tester_webengine(callback_checker, webengineview, qtbot):
"""Fixture to test javascript snippets."""
webengineview.settings().setAttribute(
QWebEngineSettings.JavascriptEnabled, True)
"""Fixture to test javascript snippets in webengine."""
return JSWebEngineTester(webengineview, callback_checker, qtbot)

View File

@ -1 +1 @@
body {background-color: rgb(0, 255, 0);}
body, :root {background-color: rgb(0, 255, 0);}

View File

@ -2,42 +2,4 @@
<!-- Copyright 2017 suve <veg@svgames.pl> -->
<component type="desktop">
<id>org.qutebrowser.qutebrowser</id>
<metadata_license>CC-BY-SA-3.0</metadata_license>
<project_license>GPL-3.0</project_license>
<name>qutebrowser</name>
<summary>A keyboard-driven web browser</summary>
<description>
<p>
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.
</p>
</description>
<categories>
<category>Network</category>
<category>WebBrowser</category>
</categories>
<provides>
<binary>qutebrowser</binary>
</provides>
<launchable type="desktop-id">qutebrowser.desktop</launchable>
<screenshots>
<screenshot type="default">
<image>https://raw.githubusercontent.com/qutebrowser/qutebrowser/master/doc/img/main.png</image>
</screenshot>
<screenshot>
<image>https://raw.githubusercontent.com/qutebrowser/qutebrowser/master/doc/img/downloads.png</image>
</screenshot>
<screenshot>
<image>https://raw.githubusercontent.com/qutebrowser/qutebrowser/master/doc/img/completion.png</image>
</screenshot>
<screenshot>
<image>https://raw.githubusercontent.com/qutebrowser/qutebrowser/master/doc/img/hints.png</image>
</screenshot>
</screenshots>
<url type="homepage">https://www.qutebrowser.org</url>
<url type="faq">https://qutebrowser.org/doc/faq.html</url>
<url type="help">https://qutebrowser.org/doc/help/</url>
<url type="bugtracker">https://github.com/qutebrowser/qutebrowser/issues/</url>
<url type="donation">https://github.com/qutebrowser/qutebrowser#donating</url>
</component>

View File

@ -1,6 +1,6 @@
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2015-2017 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
# Copyright 2017 Jay Kamat
#
# This file is part of qutebrowser.
#
@ -21,17 +21,17 @@
import os
import pytest
from qutebrowser.utils import javascript, utils
from qutebrowser.browser import shared
from qutebrowser.config import config
from PyQt5.QtWebEngineWidgets import QWebEngineSettings, QWebEngineProfile, QWebEngineScript
import qutebrowser.browser.webengine.webenginesettings as webenginesettings
from qutebrowser.utils import javascript
from PyQt5.QtWebEngineWidgets import QWebEngineProfile
from qutebrowser.browser.webengine import 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:
"""Helper class (for the stylesheet_tester fixture) for asserts.
@ -46,71 +46,76 @@ class StylesheetTester:
self.config_stub = config_stub
def init_stylesheet(self, css_file="green.css"):
self.config_stub.val.content.user_stylesheets = \
os.path.join(os.path.dirname(__file__), css_file)
"""Initialize the stylesheet with a provided css file."""
css_path = os.path.join(os.path.dirname(__file__), css_file)
self.config_stub.val.content.user_stylesheets = css_path
p = QWebEngineProfile.defaultProfile()
webenginesettings._init_stylesheet(p)
def set_css(self, css):
"""Set css to CSS via stylesheet.js."""
"""Set document style to `css` via stylesheet.js."""
code = javascript.assemble('stylesheet', 'set_css', css)
self.js.run(code, None)
def check_set(self, value, element="background-color"):
def check_set(self, value, css_style="background-color",
document_element="document.body"):
"""Check whether the css in ELEMENT is set to VALUE."""
self.js.run("window.getComputedStyle(document.body, null)"
".getPropertyValue('{}');".format(element), value)
self.js.run("window.getComputedStyle({}, null)"
".getPropertyValue('{}');".format(document_element,
css_style), value)
@pytest.fixture
def stylesheet_tester(js_tester_webengine, config_stub):
"""Helper fixture to test stylesheets"""
"""Helper fixture to test stylesheets."""
ss_tester = StylesheetTester(js_tester_webengine, config_stub)
ss_tester.js.webview.show()
return ss_tester
@pytest.mark.parametrize('page', ['stylesheet/simple.html',
'stylesheet/simple_bg_set_red.html'])
@pytest.mark.parametrize('set_js', [True, False])
def test_set_delayed(stylesheet_tester, page, set_js):
def test_set_delayed(stylesheet_tester, page):
"""Test a delayed invocation of set_css."""
stylesheet_tester.init_stylesheet("none.css")
stylesheet_tester.js.load(page)
if set_js:
stylesheet_tester.js.run(
'document.body.style.backgroundColor = "red";', 'red')
pytest.xfail("overring values set with js does not work.")
stylesheet_tester.set_css("body {background-color: rgb(0, 255, 0);}")
stylesheet_tester.check_set("rgb(0, 255, 0)")
@pytest.mark.parametrize('page', ['stylesheet/simple.html',
'stylesheet/simple_bg_set_red.html'])
def test_set_clear_bg(stylesheet_tester, page):
"""Test setting and clearing the stylesheet"""
"""Test setting and clearing the stylesheet."""
stylesheet_tester.init_stylesheet()
stylesheet_tester.js.load('stylesheet/simple.html')
stylesheet_tester.check_set(GREEN_BODY_BG)
stylesheet_tester.set_css("")
stylesheet_tester.check_set(DEFAULT_BODY_BG)
def test_no_set_xml(stylesheet_tester):
"""Test stylesheet never modifies xml files."""
stylesheet_tester.init_stylesheet()
pytest.xfail("loading xml/svg files throws exceptions")
stylesheet_tester.js.load_file('stylesheet/simple.xml')
pytest.xfail("stylesheet is set on xml documents")
stylesheet_tester.check_set(DEFAULT_BODY_BG)
stylesheet_tester.set_css("body {background-color: rgb(0, 255, 0);}")
stylesheet_tester.check_set(DEFAULT_BODY_BG)
def test_no_set_svg(stylesheet_tester):
"""Test stylesheet never modifies svg files."""
stylesheet_tester.init_stylesheet()
pytest.xfail("loading xml/svg files throws exceptions")
# pytest.xfail("loading xml/svg files throws exceptions")
stylesheet_tester.js.load_file('../../../misc/cheatsheet.svg')
stylesheet_tester.check_set(None)
pytest.xfail("stylesheet is set on svg documents??")
stylesheet_tester.check_set(DEFAULT_BODY_BG,
document_element="document.documentElement")
stylesheet_tester.set_css("body {background-color: rgb(0, 255, 0);}")
stylesheet_tester.check_set(None)
stylesheet_tester.check_set(DEFAULT_BODY_BG,
document_element="document.documentElement")
def test_set_error(stylesheet_tester):
"""Test stylesheet modifies file not found error pages."""