2017-11-07 17:52:40 +01:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2017-11-09 06:07:54 +01:00
|
|
|
# Copyright 2017 Jay Kamat
|
2017-11-07 17:52:40 +01:00
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-11-09 05:14:26 +01:00
|
|
|
"""Tests for stylesheet.js."""
|
2017-11-07 17:52:40 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
import pytest
|
2017-11-09 06:07:54 +01:00
|
|
|
from qutebrowser.utils import javascript
|
|
|
|
from PyQt5.QtWebEngineWidgets import QWebEngineProfile
|
|
|
|
from qutebrowser.browser.webengine import webenginesettings
|
|
|
|
|
2017-11-07 17:52:40 +01:00
|
|
|
|
2017-11-08 18:27:54 +01:00
|
|
|
DEFAULT_BODY_BG = "rgba(0, 0, 0, 0)"
|
2017-11-09 04:58:35 +01:00
|
|
|
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);}"
|
2017-11-08 18:27:54 +01:00
|
|
|
|
2017-11-09 06:07:54 +01:00
|
|
|
|
2017-11-07 17:52:40 +01:00
|
|
|
class StylesheetTester:
|
|
|
|
|
2017-11-09 05:14:26 +01:00
|
|
|
"""Helper class (for the stylesheet_tester fixture) for asserts.
|
2017-11-07 17:52:40 +01:00
|
|
|
|
|
|
|
Attributes:
|
|
|
|
js: The js_tester fixture.
|
2017-11-09 05:14:26 +01:00
|
|
|
config_stub: The config stub object.
|
2017-11-07 17:52:40 +01:00
|
|
|
"""
|
|
|
|
|
2017-11-09 04:58:35 +01:00
|
|
|
def __init__(self, js_tester, config_stub):
|
2017-11-07 17:52:40 +01:00
|
|
|
self.js = js_tester
|
2017-11-09 04:58:35 +01:00
|
|
|
self.config_stub = config_stub
|
2017-11-07 17:52:40 +01:00
|
|
|
|
2017-11-09 04:58:35 +01:00
|
|
|
def init_stylesheet(self, css_file="green.css"):
|
2017-11-09 06:07:54 +01:00
|
|
|
"""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
|
2017-11-09 04:58:35 +01:00
|
|
|
p = QWebEngineProfile.defaultProfile()
|
|
|
|
webenginesettings._init_stylesheet(p)
|
2017-11-08 18:27:54 +01:00
|
|
|
|
|
|
|
def set_css(self, css):
|
2017-11-09 06:07:54 +01:00
|
|
|
"""Set document style to `css` via stylesheet.js."""
|
2017-11-08 18:27:54 +01:00
|
|
|
code = javascript.assemble('stylesheet', 'set_css', css)
|
2017-11-07 17:52:40 +01:00
|
|
|
self.js.run(code, None)
|
2017-11-08 18:27:54 +01:00
|
|
|
|
2017-11-09 06:07:54 +01:00
|
|
|
def check_set(self, value, css_style="background-color",
|
|
|
|
document_element="document.body"):
|
2017-11-08 18:27:54 +01:00
|
|
|
"""Check whether the css in ELEMENT is set to VALUE."""
|
2017-11-09 06:07:54 +01:00
|
|
|
self.js.run("window.getComputedStyle({}, null)"
|
|
|
|
".getPropertyValue('{}');".format(document_element,
|
|
|
|
css_style), value)
|
2017-11-07 17:52:40 +01:00
|
|
|
|
2017-11-09 20:44:14 +01:00
|
|
|
def check_eq(self, one, two, true=True):
|
|
|
|
"""Check if one and two are equal."""
|
|
|
|
self.js.run("{} === {}".format(one, two), true)
|
|
|
|
|
2017-11-07 17:52:40 +01:00
|
|
|
|
|
|
|
@pytest.fixture
|
2017-11-09 04:58:35 +01:00
|
|
|
def stylesheet_tester(js_tester_webengine, config_stub):
|
2017-11-09 06:07:54 +01:00
|
|
|
"""Helper fixture to test stylesheets."""
|
2017-11-09 04:58:35 +01:00
|
|
|
ss_tester = StylesheetTester(js_tester_webengine, config_stub)
|
2017-11-07 17:52:40 +01:00
|
|
|
ss_tester.js.webview.show()
|
|
|
|
return ss_tester
|
|
|
|
|
2017-11-09 06:07:54 +01:00
|
|
|
|
2017-11-08 18:59:59 +01:00
|
|
|
@pytest.mark.parametrize('page', ['stylesheet/simple.html',
|
|
|
|
'stylesheet/simple_bg_set_red.html'])
|
2017-11-09 06:07:54 +01:00
|
|
|
def test_set_delayed(stylesheet_tester, page):
|
2017-11-09 05:14:26 +01:00
|
|
|
"""Test a delayed invocation of set_css."""
|
2017-11-09 04:58:35 +01:00
|
|
|
stylesheet_tester.init_stylesheet("none.css")
|
2017-11-08 18:59:59 +01:00
|
|
|
stylesheet_tester.js.load(page)
|
2017-11-09 04:58:35 +01:00
|
|
|
stylesheet_tester.set_css("body {background-color: rgb(0, 255, 0);}")
|
|
|
|
stylesheet_tester.check_set("rgb(0, 255, 0)")
|
2017-11-07 17:52:40 +01:00
|
|
|
|
2017-11-09 06:07:54 +01:00
|
|
|
|
2017-11-09 04:58:35 +01:00
|
|
|
@pytest.mark.parametrize('page', ['stylesheet/simple.html',
|
|
|
|
'stylesheet/simple_bg_set_red.html'])
|
|
|
|
def test_set_clear_bg(stylesheet_tester, page):
|
2017-11-09 06:07:54 +01:00
|
|
|
"""Test setting and clearing the stylesheet."""
|
2017-11-08 18:27:54 +01:00
|
|
|
stylesheet_tester.init_stylesheet()
|
2017-11-09 04:58:35 +01:00
|
|
|
stylesheet_tester.js.load('stylesheet/simple.html')
|
|
|
|
stylesheet_tester.check_set(GREEN_BODY_BG)
|
2017-11-08 18:27:54 +01:00
|
|
|
stylesheet_tester.set_css("")
|
2017-11-09 04:58:35 +01:00
|
|
|
stylesheet_tester.check_set(DEFAULT_BODY_BG)
|
|
|
|
|
2017-11-09 06:07:54 +01:00
|
|
|
|
2017-11-09 20:44:14 +01:00
|
|
|
def test_set_xml(stylesheet_tester):
|
|
|
|
"""Test stylesheet is applied without altering xml files."""
|
2017-11-09 04:58:35 +01:00
|
|
|
stylesheet_tester.init_stylesheet()
|
|
|
|
stylesheet_tester.js.load_file('stylesheet/simple.xml')
|
2017-11-09 20:44:14 +01:00
|
|
|
stylesheet_tester.check_set(GREEN_BODY_BG)
|
|
|
|
stylesheet_tester.check_eq("\"html\"", "document.documentElement.nodeName")
|
2017-11-09 06:07:54 +01:00
|
|
|
|
2017-11-10 18:55:27 +01:00
|
|
|
|
2017-11-09 20:44:14 +01:00
|
|
|
def test_set_svg(stylesheet_tester):
|
|
|
|
"""Test stylesheet is applied for svg files."""
|
2017-11-09 05:14:26 +01:00
|
|
|
stylesheet_tester.init_stylesheet()
|
|
|
|
stylesheet_tester.js.load_file('../../../misc/cheatsheet.svg')
|
2017-11-09 20:44:14 +01:00
|
|
|
stylesheet_tester.check_set(GREEN_BODY_BG,
|
2017-11-09 06:07:54 +01:00
|
|
|
document_element="document.documentElement")
|
2017-11-09 20:44:14 +01:00
|
|
|
stylesheet_tester.check_eq("\"svg\"", "document.documentElement.nodeName")
|
2017-11-09 06:07:54 +01:00
|
|
|
|
2017-11-09 05:46:12 +01:00
|
|
|
|
|
|
|
def test_set_error(stylesheet_tester):
|
|
|
|
"""Test stylesheet modifies file not found error pages."""
|
|
|
|
stylesheet_tester.init_stylesheet()
|
|
|
|
stylesheet_tester.js.load_file('non-existent.html', force=True)
|
|
|
|
stylesheet_tester.check_set(GREEN_BODY_BG)
|