bumblebee-status/tests/core/test_theme.py

195 lines
5 KiB
Python
Raw Normal View History

2020-06-20 14:53:44 +02:00
import pytest
2020-04-04 08:03:03 +02:00
import types
import core.theme
import core.event
2020-04-04 08:03:03 +02:00
import core.widget
import core.module
2020-06-20 14:53:44 +02:00
class SampleModule(core.module.Module):
def __init__(self, widgets, config=core.config.Config([]), theme=None):
super().__init__(config, theme, widgets)
self.name = "test"
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
@pytest.fixture(autouse=True)
def clear_events():
core.event.clear()
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
@pytest.fixture
def defaultsTheme():
return {"defaults": {"fg": "red", "bg": "black"}}
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
@pytest.fixture
def cycleTheme():
return {
"cycle": [
{"fg": "red", "bg": "black"},
{"fg": "black", "bg": "red"},
{"fg": "white", "bg": "blue"},
]
}
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
@pytest.fixture
def colorTheme():
return {"colors": [{"red": "#ff0000", "blue": "#0000ff"}]}
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
@pytest.fixture
def walTheme():
return {"colors": ["wal"]}
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
@pytest.fixture
def cycleValueTheme():
return {"defaults": {"fg": ["red", "green", "blue"]}}
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
@pytest.fixture
def stateTheme():
return {"warning": {"fg": "yellow"}, "critical": {"fg": "red"}}
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
@pytest.fixture
def overlayTheme():
return {
"load": {"prefix": "a"},
"test": {"load": {"prefix": "b"}, "prefix": "x"},
}
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_invalid_theme():
with pytest.raises(RuntimeError):
core.theme.Theme("this-theme-does-not-exist")
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_valid_theme():
theme = core.theme.Theme("default")
assert theme.name == "default"
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_defaults(defaultsTheme):
theme = core.theme.Theme(raw_data=defaultsTheme)
assert theme.get("fg") == defaultsTheme["defaults"]["fg"]
assert theme.get("bg") == defaultsTheme["defaults"]["bg"]
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_cycle(mocker, cycleTheme):
theme = core.theme.Theme(raw_data=cycleTheme)
assert theme.get("bg", "previous") == None
assert theme.get("fg") == cycleTheme["cycle"][0]["fg"]
assert theme.get("bg") == cycleTheme["cycle"][0]["bg"]
core.event.trigger("next-widget")
assert theme.get("bg", "previous") == cycleTheme["cycle"][0]["bg"]
core.event.trigger("next-widget")
assert theme.get("fg") == cycleTheme["cycle"][2]["fg"]
assert theme.get("bg") == cycleTheme["cycle"][2]["bg"]
stdout = mocker.patch("core.output.sys.stdout")
core.event.trigger("draw")
assert theme.get("fg") == cycleTheme["cycle"][0]["fg"]
assert theme.get("bg") == cycleTheme["cycle"][0]["bg"]
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_custom_iconset(defaultsTheme):
theme = core.theme.Theme(raw_data=defaultsTheme)
assert theme.get("padding") != "aaa"
assert theme.get("fg") != "blue"
theme = core.theme.Theme(
raw_data=defaultsTheme, iconset={"defaults": {"padding": "aaa", "fg": "blue"}}
)
assert theme.get("padding") == "aaa"
2020-06-20 15:11:53 +02:00
assert theme.get("fg") == "blue" # test override
2020-06-20 14:53:44 +02:00
def test_colors(defaultsTheme, colorTheme):
theme = core.theme.Theme(raw_data=defaultsTheme)
assert theme.keywords() == {}
theme = core.theme.Theme(raw_data=colorTheme)
assert theme.keywords() == colorTheme["colors"][0]
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_wal_colors(mocker, walTheme):
io = mocker.patch("core.theme.io")
os = mocker.patch("core.theme.os")
os.path.isfile.return_value = True
io.open.return_value = mocker.MagicMock()
io.open.return_value.__enter__.return_value.read.return_value = """
{ "colors": { "red": "#ff0000" } }
"""
theme = core.theme.Theme(raw_data=walTheme)
assert theme.keywords() == {"red": "#ff0000"}
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_wal_special(mocker, walTheme):
io = mocker.patch("core.theme.io")
os = mocker.patch("core.theme.os")
os.path.isfile.return_value = True
io.open.return_value.__enter__.return_value.read.return_value = """
{ "special": { "background": "#ff0000" } }
"""
theme = core.theme.Theme(raw_data=walTheme)
assert theme.keywords() == {"background": "#ff0000"}
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_cycle_value(cycleValueTheme):
widget = core.widget.Widget()
expected = cycleValueTheme["defaults"]["fg"]
theme = core.theme.Theme(raw_data=cycleValueTheme)
for i in range(0, len(expected) * 3):
2020-06-20 15:11:53 +02:00
assert theme.get("fg", widget) == expected[i % len(expected)]
2020-06-20 14:53:44 +02:00
# ensure multiple invocations are OK
2020-06-20 15:11:53 +02:00
assert theme.get("fg", widget) == expected[i % len(expected)]
2020-06-20 14:53:44 +02:00
core.event.trigger("draw")
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_state(stateTheme):
widget = core.widget.Widget()
theme = core.theme.Theme(raw_data=stateTheme)
assert theme.get("fg", widget) == None
widget.state = types.MethodType(lambda self: ["warning"], widget)
assert theme.get("fg", widget) == stateTheme["warning"]["fg"]
widget.state = types.MethodType(lambda self: ["critical"], widget)
assert theme.get("fg", widget) == stateTheme["critical"]["fg"]
2020-06-20 15:11:53 +02:00
2020-06-20 14:53:44 +02:00
def test_overlay(overlayTheme):
widget = core.widget.Widget()
module = SampleModule(widget)
theme = core.theme.Theme(raw_data=overlayTheme)
assert theme.get("prefix", widget) == overlayTheme[module.name]["prefix"]
widget.state = types.MethodType(lambda self: ["load"], widget)
assert theme.get("prefix", widget) == overlayTheme[module.name]["load"]["prefix"]
2020-04-04 08:03:03 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4