2020-02-15 14:05:27 +01:00
|
|
|
import unittest
|
2020-04-04 08:03:03 +02:00
|
|
|
import types
|
2020-02-15 14:05:27 +01:00
|
|
|
|
|
|
|
import core.theme
|
2020-02-22 13:42:44 +01:00
|
|
|
import core.event
|
2020-04-04 08:03:03 +02:00
|
|
|
import core.widget
|
2020-02-15 14:05:27 +01:00
|
|
|
|
|
|
|
class theme(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2020-02-22 13:42:44 +01:00
|
|
|
core.event.clear()
|
2020-02-15 14:05:27 +01:00
|
|
|
self.invalidThemeName = 'this-theme-does-not-exist'
|
|
|
|
self.validThemeName = 'default'
|
2020-02-16 14:02:21 +01:00
|
|
|
self.defaultsTheme = {
|
2020-02-15 14:05:27 +01:00
|
|
|
'defaults': {
|
|
|
|
'fg': 'red', 'bg': 'black'
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 14:02:21 +01:00
|
|
|
self.cycleTheme = {
|
|
|
|
'cycle': [
|
|
|
|
{ 'fg': 'red', 'bg': 'black' },
|
2020-02-16 14:39:10 +01:00
|
|
|
{ 'fg': 'black', 'bg': 'red' },
|
|
|
|
{ 'fg': 'white', 'bg': 'blue' }
|
2020-02-16 14:02:21 +01:00
|
|
|
]
|
|
|
|
}
|
2020-03-28 14:03:50 +01:00
|
|
|
self.colorTheme = {
|
|
|
|
'colors': [{
|
|
|
|
'red': '#ff0000', 'blue': '#0000ff'
|
|
|
|
}]
|
|
|
|
}
|
2020-03-28 14:51:48 +01:00
|
|
|
self.walTheme = {
|
|
|
|
'colors': ['wal']
|
|
|
|
}
|
2020-04-04 08:03:03 +02:00
|
|
|
self.cycleValueTheme = {
|
|
|
|
'defaults': {
|
|
|
|
'fg': [ 'red', 'green', 'blue' ]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.stateTheme = {
|
|
|
|
'warning': { 'fg': 'yellow' },
|
|
|
|
'critical': { 'fg': 'red' }
|
|
|
|
}
|
2020-02-15 14:05:27 +01:00
|
|
|
|
|
|
|
def test_invalid_theme(self):
|
|
|
|
with self.assertRaises(RuntimeError):
|
|
|
|
core.theme.Theme(self.invalidThemeName)
|
|
|
|
|
|
|
|
def test_valid_theme(self):
|
|
|
|
theme = core.theme.Theme(self.validThemeName)
|
|
|
|
self.assertEqual(self.validThemeName, theme.name)
|
|
|
|
|
|
|
|
def test_defaults(self):
|
2020-02-16 14:02:21 +01:00
|
|
|
theme = core.theme.Theme(raw_data=self.defaultsTheme)
|
2020-04-07 20:41:09 +02:00
|
|
|
self.assertEqual(self.defaultsTheme['defaults']['fg'], theme.get('fg'))
|
|
|
|
self.assertEqual(self.defaultsTheme['defaults']['bg'], theme.get('bg'))
|
2020-02-16 14:02:21 +01:00
|
|
|
|
|
|
|
def test_cycle(self):
|
|
|
|
theme = core.theme.Theme(raw_data=self.cycleTheme)
|
2020-04-07 20:41:09 +02:00
|
|
|
self.assertEqual(None, theme.get('prev-bg'))
|
|
|
|
self.assertEqual(self.cycleTheme['cycle'][0]['fg'], theme.get('fg'))
|
|
|
|
self.assertEqual(self.cycleTheme['cycle'][0]['bg'], theme.get('bg'))
|
2020-02-16 14:39:10 +01:00
|
|
|
core.event.trigger('next-widget')
|
2020-04-07 20:41:09 +02:00
|
|
|
self.assertEqual(self.cycleTheme['cycle'][0]['bg'], theme.get('bg', 'previous'))
|
2020-02-16 14:39:10 +01:00
|
|
|
core.event.trigger('next-widget')
|
2020-04-07 20:41:09 +02:00
|
|
|
self.assertEqual(self.cycleTheme['cycle'][2]['fg'], theme.get('fg'))
|
|
|
|
self.assertEqual(self.cycleTheme['cycle'][2]['bg'], theme.get('bg'))
|
2020-02-22 13:42:44 +01:00
|
|
|
|
|
|
|
with unittest.mock.patch('core.output.sys.stdout'):
|
|
|
|
core.event.trigger('update')
|
2020-04-07 20:41:09 +02:00
|
|
|
self.assertEqual(self.cycleTheme['cycle'][0]['fg'], theme.get('fg'))
|
|
|
|
self.assertEqual(self.cycleTheme['cycle'][0]['bg'], theme.get('bg'))
|
2020-02-15 14:05:27 +01:00
|
|
|
|
2020-03-28 14:03:50 +01:00
|
|
|
def test_custom_iconset(self):
|
|
|
|
theme = core.theme.Theme(raw_data=self.defaultsTheme)
|
2020-04-07 20:41:09 +02:00
|
|
|
self.assertNotEqual('aaa', theme.get('padding'))
|
2020-03-28 14:03:50 +01:00
|
|
|
theme = core.theme.Theme(raw_data=self.defaultsTheme, iconset={
|
|
|
|
'defaults': { 'padding': 'aaa' }
|
|
|
|
})
|
2020-04-07 20:41:09 +02:00
|
|
|
self.assertEqual('aaa', theme.get('padding'))
|
2020-03-28 14:03:50 +01:00
|
|
|
|
|
|
|
def test_colors(self):
|
|
|
|
theme = core.theme.Theme(raw_data=self.defaultsTheme)
|
|
|
|
self.assertEqual({}, theme.keywords())
|
|
|
|
theme = core.theme.Theme(raw_data=self.colorTheme)
|
|
|
|
self.assertEqual(self.colorTheme['colors'][0], theme.keywords())
|
|
|
|
|
2020-03-28 14:51:48 +01:00
|
|
|
def test_wal_colors(self):
|
|
|
|
with unittest.mock.patch('core.theme.io') as io:
|
|
|
|
io.open.return_value.__enter__.return_value.read.return_value='''
|
|
|
|
{ "colors": { "red": "#ff0000" } }
|
|
|
|
'''
|
|
|
|
|
|
|
|
theme = core.theme.Theme(raw_data=self.walTheme)
|
|
|
|
self.assertEqual({'red': '#ff0000'}, theme.keywords())
|
|
|
|
|
|
|
|
def test_wal_special(self):
|
|
|
|
with unittest.mock.patch('core.theme.io') as io:
|
|
|
|
io.open.return_value.__enter__.return_value.read.return_value='''
|
|
|
|
{ "special": { "background": "#ff0000" } }
|
|
|
|
'''
|
|
|
|
|
|
|
|
theme = core.theme.Theme(raw_data=self.walTheme)
|
|
|
|
self.assertEqual({'background': '#ff0000'}, theme.keywords())
|
|
|
|
|
2020-04-04 08:03:03 +02:00
|
|
|
def test_cycle_value(self):
|
|
|
|
widget = core.widget.Widget()
|
|
|
|
expected = self.cycleValueTheme['defaults']['fg']
|
|
|
|
theme = core.theme.Theme(raw_data=self.cycleValueTheme)
|
|
|
|
|
|
|
|
for i in range(0, len(expected)*3):
|
2020-04-07 20:41:09 +02:00
|
|
|
self.assertEqual(expected[i%len(expected)], theme.get('fg', widget))
|
2020-04-04 08:03:03 +02:00
|
|
|
|
|
|
|
def test_state(self):
|
|
|
|
widget = core.widget.Widget()
|
|
|
|
theme = core.theme.Theme(raw_data=self.stateTheme)
|
|
|
|
|
2020-04-07 20:41:09 +02:00
|
|
|
self.assertEqual(None, theme.get('fg', widget))
|
2020-04-04 08:03:03 +02:00
|
|
|
|
|
|
|
widget.state = types.MethodType(lambda self: ['warning'], widget)
|
2020-04-07 20:41:09 +02:00
|
|
|
self.assertEqual(self.stateTheme['warning']['fg'], theme.get('fg', widget))
|
2020-04-04 08:03:03 +02:00
|
|
|
|
|
|
|
widget.state = types.MethodType(lambda self: ['critical'], widget)
|
2020-04-07 20:41:09 +02:00
|
|
|
self.assertEqual(self.stateTheme['critical']['fg'], theme.get('fg', widget))
|
2020-04-04 08:03:03 +02:00
|
|
|
|
2020-02-15 14:05:27 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|