Implement proper loading of stylesheet.js

This commit is contained in:
Jay Kamat 2017-11-08 22:58:35 -05:00
parent 9a1d10ca11
commit 03eae9140e
No known key found for this signature in database
GPG Key ID: 5D2E399600F4F7B5
5 changed files with 99 additions and 34 deletions

View File

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

View File

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

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- 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

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