2015-08-19 20:43:06 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
# Copyright 2015 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/>.
|
|
|
|
|
|
|
|
"""Tests for qutebrowser.config.style."""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from PyQt5.QtCore import QObject
|
|
|
|
from PyQt5.QtGui import QColor
|
|
|
|
|
|
|
|
from qutebrowser.config import style
|
|
|
|
|
|
|
|
|
2015-10-16 16:56:31 +02:00
|
|
|
@pytest.mark.parametrize('template, expected', [
|
2015-10-18 18:42:56 +02:00
|
|
|
("{{ color['completion.bg'] }}", "black"),
|
|
|
|
("{{ color['completion.fg'] }}", "red"),
|
|
|
|
("{{ font['completion'] }}", "foo"),
|
2015-10-16 16:56:31 +02:00
|
|
|
("{{ config.get('foo', 'bar') }}", "baz"),
|
|
|
|
])
|
|
|
|
def test_get_stylesheet(config_stub, template, expected):
|
2015-08-19 20:43:06 +02:00
|
|
|
config_stub.data = {
|
2015-10-16 16:56:31 +02:00
|
|
|
'colors': {
|
|
|
|
'completion.bg': 'black',
|
|
|
|
'completion.fg': 'red',
|
|
|
|
},
|
|
|
|
'fonts': {
|
|
|
|
'completion': 'foo',
|
|
|
|
},
|
2015-08-19 20:43:06 +02:00
|
|
|
'foo': {'bar': 'baz'},
|
|
|
|
}
|
|
|
|
rendered = style.get_stylesheet(template)
|
2015-10-16 16:56:31 +02:00
|
|
|
assert rendered == expected
|
2015-08-19 20:43:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Obj(QObject):
|
|
|
|
|
|
|
|
def __init__(self, stylesheet, parent=None):
|
|
|
|
super().__init__(parent)
|
2015-08-19 21:13:35 +02:00
|
|
|
self.STYLESHEET = stylesheet # pylint: disable=invalid-name
|
2015-08-19 20:43:06 +02:00
|
|
|
self.rendered_stylesheet = None
|
|
|
|
|
|
|
|
def setStyleSheet(self, stylesheet):
|
|
|
|
self.rendered_stylesheet = stylesheet
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('delete', [True, False])
|
|
|
|
def test_set_register_stylesheet(delete, qtbot, config_stub, caplog):
|
|
|
|
config_stub.data = {'fonts': {'foo': 'bar'}, 'colors': {}}
|
|
|
|
obj = Obj("{{ font['foo'] }}")
|
|
|
|
|
2015-11-11 19:57:03 +01:00
|
|
|
with caplog.at_level(9): # VDEBUG
|
2015-08-19 20:43:06 +02:00
|
|
|
style.set_register_stylesheet(obj)
|
|
|
|
|
2015-11-11 19:57:03 +01:00
|
|
|
assert len(caplog.records) == 1
|
|
|
|
assert caplog.records[0].message == 'stylesheet for Obj: bar'
|
2015-08-19 20:43:06 +02:00
|
|
|
|
2015-10-18 02:23:48 +02:00
|
|
|
assert obj.rendered_stylesheet == 'bar'
|
2015-08-19 20:43:06 +02:00
|
|
|
|
|
|
|
if delete:
|
|
|
|
with qtbot.waitSignal(obj.destroyed):
|
|
|
|
obj.deleteLater()
|
|
|
|
|
|
|
|
config_stub.data = {'fonts': {'foo': 'baz'}, 'colors': {}}
|
|
|
|
style.get_stylesheet.cache_clear()
|
|
|
|
config_stub.changed.emit('fonts', 'foo')
|
|
|
|
|
|
|
|
if delete:
|
2015-10-18 02:23:48 +02:00
|
|
|
expected = 'bar'
|
2015-08-19 20:43:06 +02:00
|
|
|
else:
|
2015-10-18 02:23:48 +02:00
|
|
|
expected = 'baz'
|
2015-08-19 20:43:06 +02:00
|
|
|
assert obj.rendered_stylesheet == expected
|
|
|
|
|
|
|
|
|
|
|
|
class TestColorDict:
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('key, expected', [
|
|
|
|
('foo', 'one'),
|
2015-10-18 17:41:15 +02:00
|
|
|
('foo.fg', 'two'),
|
|
|
|
('foo.bg', 'three'),
|
2015-08-19 20:43:06 +02:00
|
|
|
])
|
|
|
|
def test_values(self, key, expected):
|
|
|
|
d = style.ColorDict()
|
|
|
|
d['foo'] = 'one'
|
2015-10-18 17:41:15 +02:00
|
|
|
d['foo.fg'] = 'two'
|
|
|
|
d['foo.bg'] = 'three'
|
2015-08-19 20:43:06 +02:00
|
|
|
assert d[key] == expected
|
|
|
|
|
|
|
|
def test_key_error(self, caplog):
|
|
|
|
d = style.ColorDict()
|
2015-11-11 19:57:03 +01:00
|
|
|
with caplog.at_level(logging.ERROR):
|
2015-08-19 21:13:35 +02:00
|
|
|
d['foo'] # pylint: disable=pointless-statement
|
2015-11-11 19:57:03 +01:00
|
|
|
assert len(caplog.records) == 1
|
|
|
|
assert caplog.records[0].message == 'No color defined for foo!'
|
2015-08-19 20:43:06 +02:00
|
|
|
|
|
|
|
def test_qcolor(self):
|
|
|
|
d = style.ColorDict()
|
2015-08-19 20:48:19 +02:00
|
|
|
d['foo'] = QColor()
|
2015-08-19 20:43:06 +02:00
|
|
|
with pytest.raises(TypeError):
|
2015-08-19 21:13:35 +02:00
|
|
|
d['foo'] # pylint: disable=pointless-statement
|