diff options
author | Florian Bruhin <me@the-compiler.org> | 2019-12-20 14:06:42 +0100 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2019-12-20 16:54:08 +0100 |
commit | 7b87f6760f200add7e9eac9e8796b25e710ffbd6 (patch) | |
tree | c6c558a47867362df863c603df526d5615699344 | |
parent | 20ee9e2d09b8a3d275080000b11fd5566afd525c (diff) | |
download | qutebrowser-7b87f6760f200add7e9eac9e8796b25e710ffbd6.tar.gz qutebrowser-7b87f6760f200add7e9eac9e8796b25e710ffbd6.zip |
Split config.stylesheet into separate module
This avoids circular imports such as:
urlutils -> config -> jinja -> urlutils
-rw-r--r-- | qutebrowser/browser/downloadview.py | 4 | ||||
-rw-r--r-- | qutebrowser/browser/webkit/webview.py | 4 | ||||
-rw-r--r-- | qutebrowser/completion/completionwidget.py | 4 | ||||
-rw-r--r-- | qutebrowser/config/config.py | 92 | ||||
-rw-r--r-- | qutebrowser/config/configinit.py | 4 | ||||
-rw-r--r-- | qutebrowser/config/stylesheet.py | 116 | ||||
-rw-r--r-- | qutebrowser/mainwindow/mainwindow.py | 4 | ||||
-rw-r--r-- | qutebrowser/mainwindow/messageview.py | 13 | ||||
-rw-r--r-- | qutebrowser/mainwindow/prompt.py | 4 | ||||
-rw-r--r-- | qutebrowser/mainwindow/statusbar/bar.py | 14 | ||||
-rw-r--r-- | qutebrowser/mainwindow/statusbar/progress.py | 4 | ||||
-rw-r--r-- | qutebrowser/mainwindow/statusbar/url.py | 4 | ||||
-rw-r--r-- | qutebrowser/mainwindow/tabwidget.py | 4 | ||||
-rw-r--r-- | qutebrowser/misc/keyhintwidget.py | 4 | ||||
-rw-r--r-- | tests/helpers/fixtures.py | 5 | ||||
-rw-r--r-- | tests/unit/config/test_config.py | 56 | ||||
-rw-r--r-- | tests/unit/config/test_stylesheet.py | 72 | ||||
-rw-r--r-- | tests/unit/javascript/stylesheet/test_stylesheet_js.py (renamed from tests/unit/javascript/stylesheet/test_stylesheet.py) | 0 |
18 files changed, 231 insertions, 177 deletions
diff --git a/qutebrowser/browser/downloadview.py b/qutebrowser/browser/downloadview.py index 91a583d08..b0944046b 100644 --- a/qutebrowser/browser/downloadview.py +++ b/qutebrowser/browser/downloadview.py @@ -26,7 +26,7 @@ from PyQt5.QtCore import pyqtSlot, QSize, Qt, QTimer from PyQt5.QtWidgets import QListView, QSizePolicy, QMenu, QStyleFactory from qutebrowser.browser import downloads -from qutebrowser.config import config +from qutebrowser.config import stylesheet from qutebrowser.utils import qtutils, utils from qutebrowser.qt import sip @@ -85,7 +85,7 @@ class DownloadView(QListView): super().__init__(parent) if not utils.is_mac: self.setStyle(QStyleFactory.create('Fusion')) - config.set_register_stylesheet(self) + stylesheet.set_register(self) self.setResizeMode(QListView.Adjust) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed) diff --git a/qutebrowser/browser/webkit/webview.py b/qutebrowser/browser/webkit/webview.py index 1cd9d0a2a..88daf06aa 100644 --- a/qutebrowser/browser/webkit/webview.py +++ b/qutebrowser/browser/webkit/webview.py @@ -24,7 +24,7 @@ from PyQt5.QtWidgets import QStyleFactory from PyQt5.QtWebKit import QWebSettings from PyQt5.QtWebKitWidgets import QWebView, QWebPage -from qutebrowser.config import config +from qutebrowser.config import config, stylesheet from qutebrowser.keyinput import modeman from qutebrowser.utils import log, usertypes, utils, objreg, debug from qutebrowser.browser.webkit import webpage @@ -84,7 +84,7 @@ class WebView(QWebView): self.setPage(page) - config.set_register_stylesheet(self) + stylesheet.set_register(self) def __repr__(self): urlstr = self.url().toDisplayString(QUrl.EncodeUnicode) # type: ignore diff --git a/qutebrowser/completion/completionwidget.py b/qutebrowser/completion/completionwidget.py index 4d90a4305..5b1b080e4 100644 --- a/qutebrowser/completion/completionwidget.py +++ b/qutebrowser/completion/completionwidget.py @@ -28,7 +28,7 @@ import typing from PyQt5.QtWidgets import QTreeView, QSizePolicy, QStyleFactory, QWidget from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QItemSelectionModel, QSize -from qutebrowser.config import config +from qutebrowser.config import config, stylesheet from qutebrowser.completion import completiondelegate from qutebrowser.utils import utils, usertypes, debug, log from qutebrowser.api import cmdutils @@ -125,7 +125,7 @@ class CompletionView(QTreeView): self._delegate = completiondelegate.CompletionItemDelegate(self) self.setItemDelegate(self._delegate) self.setStyle(QStyleFactory.create('Fusion')) - config.set_register_stylesheet(self) + stylesheet.set_register(self) self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) self.setHeaderHidden(True) self.setAlternatingRowColors(True) diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index d8e0e110a..41ef6eb3d 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -23,13 +23,13 @@ import copy import contextlib import functools import typing -from typing import Any, Optional, FrozenSet +from typing import Any, Optional -from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QUrl +from PyQt5.QtCore import pyqtSignal, QObject, QUrl from qutebrowser.config import configdata, configexc, configutils -from qutebrowser.utils import utils, log, jinja, urlmatch -from qutebrowser.misc import objects, debugcachestats +from qutebrowser.utils import utils, log, urlmatch +from qutebrowser.misc import objects from qutebrowser.keyinput import keyutils if typing.TYPE_CHECKING: @@ -278,7 +278,6 @@ class Config(QObject): yaml_config: 'configfiles.YamlConfig', parent: QObject = None) -> None: super().__init__(parent) - self.changed.connect(_render_stylesheet.cache_clear) self._mutables = {} # type: MutableMapping[str, Tuple[Any, Any]] self._yaml = yaml_config self._init_values() @@ -617,86 +616,3 @@ class ConfigContainer: return '{}.{}'.format(self._prefix, attr) else: return attr - - -def set_register_stylesheet(obj: QObject, *, - stylesheet: str = None, - update: bool = True) -> None: - """Set the stylesheet for an object. - - Also, register an update when the config is changed. - - Args: - obj: The object to set the stylesheet for and register. - Must have a STYLESHEET attribute if stylesheet is not given. - stylesheet: The stylesheet to use. - update: Whether to update the stylesheet on config changes. - """ - observer = StyleSheetObserver(obj, stylesheet, update) - observer.register() - - -@debugcachestats.register() -@functools.lru_cache() -def _render_stylesheet(stylesheet: str) -> str: - """Render the given stylesheet jinja template.""" - with jinja.environment.no_autoescape(): - template = jinja.environment.from_string(stylesheet) - return template.render(conf=val) - - -class StyleSheetObserver(QObject): - - """Set the stylesheet on the given object and update it on changes. - - Attributes: - _obj: The object to observe. - _stylesheet: The stylesheet template to use. - _options: The config options that the stylesheet uses. When it's not - necessary to listen for config changes, this attribute may be - None. - """ - - def __init__(self, obj: QObject, - stylesheet: Optional[str], update: bool) -> None: - super().__init__() - self._obj = obj - self._update = update - - # We only need to hang around if we are asked to update. - if update: - self.setParent(self._obj) - if stylesheet is None: - self._stylesheet = obj.STYLESHEET # type: str - else: - self._stylesheet = stylesheet - - if update: - self._options = jinja.template_config_variables( - self._stylesheet) # type: Optional[FrozenSet[str]] - else: - self._options = None - - def _get_stylesheet(self) -> str: - """Format a stylesheet based on a template. - - Return: - The formatted template as string. - """ - return _render_stylesheet(self._stylesheet) - - @pyqtSlot(str) - def _maybe_update_stylesheet(self, option: str) -> None: - """Update the stylesheet for obj if the option changed affects it.""" - assert self._options is not None - if option in self._options: - self._obj.setStyleSheet(self._get_stylesheet()) - - def register(self) -> None: - """Do a first update and listen for more.""" - qss = self._get_stylesheet() - log.config.vdebug( # type: ignore - "stylesheet for {}: {}".format(self._obj.__class__.__name__, qss)) - self._obj.setStyleSheet(qss) - if self._update: - instance.changed.connect(self._maybe_update_stylesheet) diff --git a/qutebrowser/config/configinit.py b/qutebrowser/config/configinit.py index 21f7fa054..e2c359e72 100644 --- a/qutebrowser/config/configinit.py +++ b/qutebrowser/config/configinit.py @@ -28,7 +28,7 @@ from PyQt5.QtWidgets import QMessageBox from qutebrowser.api import config as configapi from qutebrowser.config import (config, configdata, configfiles, configtypes, - configexc, configcommands) + configexc, configcommands, stylesheet) from qutebrowser.utils import (objreg, usertypes, log, standarddir, message, qtutils) from qutebrowser.config import configcache @@ -88,6 +88,8 @@ def early_init(args: argparse.Namespace) -> None: configtypes.Font.monospace_fonts = config.val.fonts.monospace config.instance.changed.connect(_update_monospace_fonts) + stylesheet.init() + _init_envvars() diff --git a/qutebrowser/config/stylesheet.py b/qutebrowser/config/stylesheet.py new file mode 100644 index 000000000..27432d01b --- /dev/null +++ b/qutebrowser/config/stylesheet.py @@ -0,0 +1,116 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2019 Florian Bruhin (The Compiler) <mail@qutebrowser.org> +# +# 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/>. + +"""Handling of Qt qss stylesheets.""" + +import functools +from typing import Optional, FrozenSet + +from PyQt5.QtCore import pyqtSlot, QObject + +from qutebrowser.config import config +from qutebrowser.misc import debugcachestats +from qutebrowser.utils import jinja, log + + +def set_register(obj: QObject, + stylesheet: str = None, *, + update: bool = True) -> None: + """Set the stylesheet for an object. + + Also, register an update when the config is changed. + + Args: + obj: The object to set the stylesheet for and register. + Must have a STYLESHEET attribute if stylesheet is not given. + stylesheet: The stylesheet to use. + update: Whether to update the stylesheet on config changes. + """ + observer = _StyleSheetObserver(obj, stylesheet, update) + observer.register() + + +@debugcachestats.register() +@functools.lru_cache() +def _render_stylesheet(stylesheet: str) -> str: + """Render the given stylesheet jinja template.""" + with jinja.environment.no_autoescape(): + template = jinja.environment.from_string(stylesheet) + return template.render(conf=config.val) + + +def init() -> None: + config.instance.changed.connect(_render_stylesheet.cache_clear) + + +class _StyleSheetObserver(QObject): + + """Set the stylesheet on the given object and update it on changes. + + Attributes: + _obj: The object to observe. + _stylesheet: The stylesheet template to use. + _options: The config options that the stylesheet uses. When it's not + necessary to listen for config changes, this attribute may be + None. + """ + + def __init__(self, obj: QObject, + stylesheet: Optional[str], update: bool) -> None: + super().__init__() + self._obj = obj + self._update = update + + # We only need to hang around if we are asked to update. + if update: + self.setParent(self._obj) + if stylesheet is None: + self._stylesheet = obj.STYLESHEET # type: str + else: + self._stylesheet = stylesheet + + if update: + self._options = jinja.template_config_variables( + self._stylesheet) # type: Optional[FrozenSet[str]] + else: + self._options = None + + def _get_stylesheet(self) -> str: + """Format a stylesheet based on a template. + + Return: + The formatted template as string. + """ + return _render_stylesheet(self._stylesheet) + + @pyqtSlot(str) + def _maybe_update_stylesheet(self, option: str) -> None: + """Update the stylesheet for obj if the option changed affects it.""" + assert self._options is not None + if option in self._options: + self._obj.setStyleSheet(self._get_stylesheet()) + + def register(self) -> None: + """Do a first update and listen for more.""" + qss = self._get_stylesheet() + log.config.vdebug( # type: ignore + "stylesheet for {}: {}".format(self._obj.__class__.__name__, qss)) + self._obj.setStyleSheet(qss) + if self._update: + config.instance.changed.connect(self._maybe_update_stylesheet) diff --git a/qutebrowser/mainwindow/mainwindow.py b/qutebrowser/mainwindow/mainwindow.py index ac5df12e6..e0f047bfb 100644 --- a/qutebrowser/mainwindow/mainwindow.py +++ b/qutebrowser/mainwindow/mainwindow.py @@ -31,7 +31,7 @@ from PyQt5.QtWidgets import QWidget, QVBoxLayout, QApplication, QSizePolicy from qutebrowser.commands import runners from qutebrowser.api import cmdutils -from qutebrowser.config import config, configfiles +from qutebrowser.config import config, configfiles, stylesheet from qutebrowser.utils import (message, log, usertypes, qtutils, objreg, utils, jinja, debug) from qutebrowser.mainwindow import messageview, prompt @@ -269,7 +269,7 @@ class MainWindow(QWidget): self._set_decoration(config.val.window.hide_decoration) self.state_before_fullscreen = self.windowState() - config.set_register_stylesheet(self) + stylesheet.set_register(self) def _init_geometry(self, geometry): """Initialize the window geometry or load it from disk.""" diff --git a/qutebrowser/mainwindow/messageview.py b/qutebrowser/mainwindow/messageview.py index ea14265aa..3660d3529 100644 --- a/qutebrowser/mainwindow/messageview.py +++ b/qutebrowser/mainwindow/messageview.py @@ -24,7 +24,7 @@ import typing from PyQt5.QtCore import pyqtSlot, pyqtSignal, QTimer, Qt, QSize from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QSizePolicy -from qutebrowser.config import config +from qutebrowser.config import config, stylesheet from qutebrowser.utils import usertypes @@ -36,19 +36,19 @@ class Message(QLabel): super().__init__(text, parent) self.replace = replace self.setAttribute(Qt.WA_StyledBackground, True) - stylesheet = """ + qss = """ padding-top: 2px; padding-bottom: 2px; """ if level == usertypes.MessageLevel.error: - stylesheet += """ + qss += """ background-color: {{ conf.colors.messages.error.bg }}; color: {{ conf.colors.messages.error.fg }}; font: {{ conf.fonts.messages.error }}; border-bottom: 1px solid {{ conf.colors.messages.error.border }}; """ elif level == usertypes.MessageLevel.warning: - stylesheet += """ + qss += """ background-color: {{ conf.colors.messages.warning.bg }}; color: {{ conf.colors.messages.warning.fg }}; font: {{ conf.fonts.messages.warning }}; @@ -56,7 +56,7 @@ class Message(QLabel): 1px solid {{ conf.colors.messages.warning.border }}; """ elif level == usertypes.MessageLevel.info: - stylesheet += """ + qss += """ background-color: {{ conf.colors.messages.info.bg }}; color: {{ conf.colors.messages.info.fg }}; font: {{ conf.fonts.messages.info }}; @@ -66,8 +66,7 @@ class Message(QLabel): raise ValueError("Invalid level {!r}".format(level)) # We don't bother with set_register_stylesheet here as it's short-lived # anyways. - config.set_register_stylesheet(self, stylesheet=stylesheet, - update=False) + stylesheet.set_register(self, qss, update=False) class MessageView(QWidget): diff --git a/qutebrowser/mainwindow/prompt.py b/qutebrowser/mainwindow/prompt.py index 6f7d1b9f7..ba6ff2f1b 100644 --- a/qutebrowser/mainwindow/prompt.py +++ b/qutebrowser/mainwindow/prompt.py @@ -33,7 +33,7 @@ from PyQt5.QtWidgets import (QWidget, QGridLayout, QVBoxLayout, QLineEdit, QSpacerItem) from qutebrowser.browser import downloads -from qutebrowser.config import config, configtypes, configexc +from qutebrowser.config import config, configtypes, configexc, stylesheet from qutebrowser.utils import usertypes, log, utils, qtutils, objreg, message from qutebrowser.keyinput import modeman from qutebrowser.api import cmdutils @@ -292,7 +292,7 @@ class PromptContainer(QWidget): self.setObjectName('PromptContainer') self.setAttribute(Qt.WA_StyledBackground, True) - config.set_register_stylesheet(self) + stylesheet.set_register(self) message.global_bridge.prompt_done.connect(self._on_prompt_done) prompt_queue.show_prompts.connect(self._on_show_prompts) diff --git a/qutebrowser/mainwindow/statusbar/bar.py b/qutebrowser/mainwindow/statusbar/bar.py index 463e0c151..dd50024b3 100644 --- a/qutebrowser/mainwindow/statusbar/bar.py +++ b/qutebrowser/mainwindow/statusbar/bar.py @@ -25,7 +25,7 @@ from PyQt5.QtCore import pyqtSignal, pyqtSlot, pyqtProperty, Qt, QSize, QTimer from PyQt5.QtWidgets import QWidget, QHBoxLayout, QStackedLayout, QSizePolicy from qutebrowser.browser import browsertab -from qutebrowser.config import config +from qutebrowser.config import config, stylesheet from qutebrowser.keyinput import modeman from qutebrowser.utils import usertypes, log, objreg, utils from qutebrowser.mainwindow.statusbar import (backforward, command, progress, @@ -97,7 +97,7 @@ def _generate_stylesheet(): ('passthrough', 'statusbar.passthrough'), ('private-command', 'statusbar.command.private'), ] - stylesheet = """ + qss = """ QWidget#StatusBar, QWidget#StatusBar QLabel, QWidget#StatusBar QLineEdit { @@ -110,7 +110,7 @@ def _generate_stylesheet(): } """ for flag, option in flags: - stylesheet += """ + qss += """ QWidget#StatusBar[color_flags~="%s"], QWidget#StatusBar[color_flags~="%s"] QLabel, QWidget#StatusBar[color_flags~="%s"] QLineEdit { @@ -122,7 +122,7 @@ def _generate_stylesheet(): } """ % (flag, flag, flag, # noqa: S001 option + '.fg', flag, option + '.bg') - return stylesheet + return qss class StatusBar(QWidget): @@ -158,7 +158,7 @@ class StatusBar(QWidget): super().__init__(parent) self.setObjectName(self.__class__.__name__) self.setAttribute(Qt.WA_StyledBackground) - config.set_register_stylesheet(self) + stylesheet.set_register(self) self.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Fixed) @@ -298,7 +298,7 @@ class StatusBar(QWidget): # Turning on is handled in on_current_caret_selection_toggled log.statusbar.debug("Setting caret mode off") self._color_flags.caret = ColorFlags.CaretMode.off - config.set_register_stylesheet(self, update=False) + stylesheet.set_register(self, update=False) def _set_mode_text(self, mode): """Set the mode text.""" @@ -382,7 +382,7 @@ class StatusBar(QWidget): else: self._set_mode_text("caret") self._color_flags.caret = ColorFlags.CaretMode.on - config.set_register_stylesheet(self, update=False) + stylesheet.set_register(self, update=False) def resizeEvent(self, e): """Extend resizeEvent of QWidget to emit a resized signal afterwards. diff --git a/qutebrowser/mainwindow/statusbar/progress.py b/qutebrowser/mainwindow/statusbar/progress.py index 34c1954e8..389ddf12e 100644 --- a/qutebrowser/mainwindow/statusbar/progress.py +++ b/qutebrowser/mainwindow/statusbar/progress.py @@ -22,7 +22,7 @@ from PyQt5.QtCore import pyqtSlot, QSize from PyQt5.QtWidgets import QProgressBar, QSizePolicy -from qutebrowser.config import config +from qutebrowser.config import stylesheet from qutebrowser.utils import utils, usertypes @@ -45,7 +45,7 @@ class Progress(QProgressBar): def __init__(self, parent=None): super().__init__(parent) - config.set_register_stylesheet(self) + stylesheet.set_register(self) self.enabled = False self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) self.setTextVisible(False) diff --git a/qutebrowser/mainwindow/statusbar/url.py b/qutebrowser/mainwindow/statusbar/url.py index d6aacb184..7a84b1e4e 100644 --- a/qutebrowser/mainwindow/statusbar/url.py +++ b/qutebrowser/mainwindow/statusbar/url.py @@ -24,7 +24,7 @@ import enum from PyQt5.QtCore import pyqtSlot, pyqtProperty, QUrl from qutebrowser.mainwindow.statusbar import textbase -from qutebrowser.config import config +from qutebrowser.config import stylesheet from qutebrowser.utils import usertypes, urlutils @@ -76,7 +76,7 @@ class UrlText(textbase.TextBase): super().__init__(parent) self._urltype = None self.setObjectName(self.__class__.__name__) - config.set_register_stylesheet(self) + stylesheet.set_register(self) self._hover_url = None self._normal_url = None self._normal_url_type = UrlType.normal diff --git a/qutebrowser/mainwindow/tabwidget.py b/qutebrowser/mainwindow/tabwidget.py index ba6ffb79f..6e3c00aad 100644 --- a/qutebrowser/mainwindow/tabwidget.py +++ b/qutebrowser/mainwindow/tabwidget.py @@ -32,7 +32,7 @@ from PyQt5.QtWidgets import (QTabWidget, QTabBar, QSizePolicy, QCommonStyle, from PyQt5.QtGui import QIcon, QPalette, QColor from qutebrowser.utils import qtutils, objreg, utils, usertypes, log -from qutebrowser.config import config +from qutebrowser.config import config, stylesheet from qutebrowser.misc import objects, debugcachestats from qutebrowser.browser import browsertab @@ -400,7 +400,7 @@ class TabBar(QTabBar): self._on_show_switching_delay_changed() self.setAutoFillBackground(True) self.drag_in_progress = False - config.set_register_stylesheet(self) + stylesheet.set_register(self) QTimer.singleShot(0, self.maybe_hide) def __repr__(self): diff --git a/qutebrowser/misc/keyhintwidget.py b/qutebrowser/misc/keyhintwidget.py index 9cc00c6a8..68f9685d3 100644 --- a/qutebrowser/misc/keyhintwidget.py +++ b/qutebrowser/misc/keyhintwidget.py @@ -31,7 +31,7 @@ import re from PyQt5.QtWidgets import QLabel, QSizePolicy from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt -from qutebrowser.config import config +from qutebrowser.config import config, stylesheet from qutebrowser.utils import utils, usertypes from qutebrowser.misc import objects from qutebrowser.keyinput import keyutils @@ -72,7 +72,7 @@ class KeyHintView(QLabel): self._show_timer = usertypes.Timer(self, 'keyhint_show') self._show_timer.timeout.connect(self.show) self._show_timer.setSingleShot(True) - config.set_register_stylesheet(self) + stylesheet.set_register(self) def __repr__(self): return utils.get_repr(self, win_id=self._win_id) diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py index 880438d93..045be5e94 100644 --- a/tests/helpers/fixtures.py +++ b/tests/helpers/fixtures.py @@ -43,7 +43,7 @@ from PyQt5.QtNetwork import QNetworkCookieJar import helpers.stubs as stubsmod from qutebrowser.config import (config, configdata, configtypes, configexc, - configfiles, configcache) + configfiles, configcache, stylesheet) from qutebrowser.api import config as configapi from qutebrowser.utils import objreg, standarddir, utils, usertypes from qutebrowser.browser import greasemonkey, history, qutescheme @@ -319,6 +319,9 @@ def config_stub(stubs, monkeypatch, configdata_init, yaml_config_stub): pass conf.val = container # For easier use in tests + + stylesheet.init() + return conf diff --git a/tests/unit/config/test_config.py b/tests/unit/config/test_config.py index e8e814a79..d81cb91dd 100644 --- a/tests/unit/config/test_config.py +++ b/tests/unit/config/test_config.py @@ -23,7 +23,7 @@ import unittest.mock import functools import pytest -from PyQt5.QtCore import QObject, QUrl +from PyQt5.QtCore import QUrl from PyQt5.QtGui import QColor from qutebrowser.config import config, configdata, configexc @@ -766,57 +766,3 @@ class TestContainer: with pytest.raises(TypeError, match="Can't use pattern without configapi!"): config.ConfigContainer(config_stub, pattern=pattern) - - -class StyleObj(QObject): - - def __init__(self, stylesheet=None, parent=None): - super().__init__(parent) - if stylesheet is not None: - self.STYLESHEET = stylesheet # noqa: N801,N806 pylint: disable=invalid-name - self.rendered_stylesheet = None - - def setStyleSheet(self, stylesheet): - self.rendered_stylesheet = stylesheet - - -def test_get_stylesheet(config_stub): - config_stub.val.colors.hints.fg = 'magenta' - observer = config.StyleSheetObserver( - StyleObj(), stylesheet="{{ conf.colors.hints.fg }}", update=False) - assert observer._get_stylesheet() == 'magenta' - - -@pytest.mark.parametrize('delete', [True, False]) -@pytest.mark.parametrize('stylesheet_param', [True, False]) -@pytest.mark.parametrize('update', [True, False]) -def test_set_register_stylesheet(delete, stylesheet_param, update, qtbot, - config_stub, caplog): - config_stub.val.colors.hints.fg = 'magenta' - stylesheet = "{{ conf.colors.hints.fg }}" - - with caplog.at_level(9): # VDEBUG - if stylesheet_param: - obj = StyleObj() - config.set_register_stylesheet(obj, stylesheet=stylesheet, - update=update) - else: - obj = StyleObj(stylesheet) - config.set_register_stylesheet(obj, update=update) - - assert caplog.messages[-1] == 'stylesheet for StyleObj: magenta' - - assert obj.rendered_stylesheet == 'magenta' - - if delete: - with qtbot.waitSignal(obj.destroyed): - obj.deleteLater() - - config_stub.val.colors.hints.fg = 'yellow' - - if delete or not update: - expected = 'magenta' - else: - expected = 'yellow' - - assert obj.rendered_stylesheet == expected diff --git a/tests/unit/config/test_stylesheet.py b/tests/unit/config/test_stylesheet.py new file mode 100644 index 000000000..67bdd04b4 --- /dev/null +++ b/tests/unit/config/test_stylesheet.py @@ -0,0 +1,72 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: +# Copyright 2019 Florian Bruhin (The Compiler) <mail@qutebrowser.org> + +# 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/>. + +import pytest + +from PyQt5.QtCore import QObject + +from qutebrowser.config import stylesheet + + +class StyleObj(QObject): + + def __init__(self, stylesheet=None, parent=None): + super().__init__(parent) + if stylesheet is not None: + self.STYLESHEET = stylesheet # noqa: N801,N806 pylint: disable=invalid-name + self.rendered_stylesheet = None + + def setStyleSheet(self, stylesheet): + self.rendered_stylesheet = stylesheet + + +def test_get_stylesheet(config_stub): + config_stub.val.colors.hints.fg = 'magenta' + observer = stylesheet._StyleSheetObserver( + StyleObj(), stylesheet="{{ conf.colors.hints.fg }}", update=False) + assert observer._get_stylesheet() == 'magenta' + + +@pytest.mark.parametrize('delete', [True, False]) +@pytest.mark.parametrize('stylesheet_param', [True, False]) +@pytest.mark.parametrize('update', [True, False]) +def test_set_register_stylesheet(delete, stylesheet_param, update, qtbot, + config_stub, caplog): + config_stub.val.colors.hints.fg = 'magenta' + qss = "{{ conf.colors.hints.fg }}" + + with caplog.at_level(9): # VDEBUG + if stylesheet_param: + obj = StyleObj() + stylesheet.set_register(obj, qss, update=update) + else: + obj = StyleObj(qss) + stylesheet.set_register(obj, update=update) + + assert caplog.messages[-1] == 'stylesheet for StyleObj: magenta' + + assert obj.rendered_stylesheet == 'magenta' + + if delete: + with qtbot.waitSignal(obj.destroyed): + obj.deleteLater() + + config_stub.val.colors.hints.fg = 'yellow' + + expected = 'magenta' if delete or not update else 'yellow' + assert obj.rendered_stylesheet == expected diff --git a/tests/unit/javascript/stylesheet/test_stylesheet.py b/tests/unit/javascript/stylesheet/test_stylesheet_js.py index 768ffaeb9..768ffaeb9 100644 --- a/tests/unit/javascript/stylesheet/test_stylesheet.py +++ b/tests/unit/javascript/stylesheet/test_stylesheet_js.py |