1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
import datetime
import sys
from importlib import reload
import pytest
import libqtile.config
from libqtile.widget import clock
from test.widgets.conftest import FakeBar
def no_op(*args, **kwargs):
pass
# Mock Datetime object that returns a set datetime and also
# has a simplified timezone method to check functionality of
# the widget.
class MockDatetime(datetime.datetime):
@classmethod
def now(cls, *args, **kwargs):
return cls(2021, 1, 1, 10, 20, 30)
def astimezone(self, tzone=None):
if tzone is None:
return self
return self + tzone.utcoffset(None)
@pytest.fixture
def patched_clock(monkeypatch):
# Stop system importing these modules in case they exist on environment
monkeypatch.setitem(sys.modules, "pytz", None)
monkeypatch.setitem(sys.modules, "dateutil", None)
monkeypatch.setitem(sys.modules, "dateutil.tz", None)
# Reload module to force ImportErrors
reload(clock)
# Override datetime.
# This is key for testing as we can fix time.
monkeypatch.setattr("libqtile.widget.clock.datetime", MockDatetime)
def test_clock(fake_qtile, monkeypatch, fake_window):
"""test clock output with default settings"""
monkeypatch.setattr("libqtile.widget.clock.datetime", MockDatetime)
clk1 = clock.Clock()
fakebar = FakeBar([clk1], window=fake_window)
clk1._configure(fake_qtile, fakebar)
text = clk1.poll()
assert text == "10:20"
@pytest.mark.usefixtures("patched_clock")
def test_clock_invalid_timezone(fake_qtile, monkeypatch, fake_window, caplog):
"""test clock widget with invalid timezone (and no pytz or dateutil modules)"""
class FakeDateutilTZ:
@classmethod
def tz(cls):
return cls
@classmethod
def gettz(cls, val):
return None
# pytz and dateutil must not be in the sys.modules dict...
monkeypatch.delitem(sys.modules, "pytz")
monkeypatch.delitem(sys.modules, "dateutil")
# Set up references to pytz and dateutil so we know these aren't being used
# If they're called, the widget would try to run None(self.timezone) which
# would raise an exception
clock.pytz = None
clock.dateutil = FakeDateutilTZ
# Fake datetime module just adds the timezone value to the time
clk2 = clock.Clock(timezone="1")
fakebar = FakeBar([clk2], window=fake_window)
clk2._configure(fake_qtile, fakebar)
# An invalid timezone results in a log message
assert (
"Clock widget can not infer its timezone from a string without pytz or dateutil."
in caplog.text
)
@pytest.mark.usefixtures("patched_clock")
def test_clock_datetime_timezone(fake_qtile, monkeypatch, fake_window):
"""test clock with datetime timezone"""
class FakeDateutilTZ:
class TZ:
@classmethod
def gettz(cls, val):
None
tz = TZ
# Set up references to pytz and dateutil so we know these aren't being used
# If they're called, the widget would try to run None(self.timezone) which
# would raise an exception
clock.pytz = None
clock.dateutil = FakeDateutilTZ
# Fake datetime module just adds the timezone value to the time
tz = datetime.timezone(datetime.timedelta(hours=1))
clk3 = clock.Clock(timezone=tz)
fakebar = FakeBar([clk3], window=fake_window)
clk3._configure(fake_qtile, fakebar)
text = clk3.poll()
# Default time is 10:20 and we add 1 hour for the timezone
assert text == "11:20"
@pytest.mark.usefixtures("patched_clock")
def test_clock_pytz_timezone(fake_qtile, monkeypatch, fake_window):
"""test clock with pytz timezone"""
class FakeDateutilTZ:
class TZ:
@classmethod
def gettz(cls, val):
None
tz = TZ
class FakePytz:
# pytz timezone is a string so convert it to an int and add 1
# to show that this code is being run
@classmethod
def timezone(cls, value):
hours = int(value) + 1
return datetime.timezone(datetime.timedelta(hours=hours))
# We need pytz in the sys.modules dict
monkeypatch.setitem(sys.modules, "pytz", True)
# Set up references to pytz and dateutil so we know these aren't being used
# If they're called, the widget would try to run None(self.timezone) which
# would raise an exception
clock.pytz = FakePytz
clock.dateutil = FakeDateutilTZ
# Pytz timezone must be a string
clk4 = clock.Clock(timezone="1")
fakebar = FakeBar([clk4], window=fake_window)
clk4._configure(fake_qtile, fakebar)
text = clk4.poll()
# Default time is 10:20 and we add 1 hour for the timezone plus and extra
# 1 for the pytz function
assert text == "12:20"
@pytest.mark.usefixtures("patched_clock")
def test_clock_dateutil_timezone(fake_qtile, monkeypatch, fake_window):
"""test clock with dateutil timezone"""
class FakeDateutilTZ:
class TZ:
@classmethod
def gettz(cls, val):
hours = int(val) + 2
return datetime.timezone(datetime.timedelta(hours=hours))
tz = TZ
# pytz must not be in the sys.modules dict...
monkeypatch.delitem(sys.modules, "pytz")
# ...but dateutil must be
monkeypatch.setitem(sys.modules, "dateutil", True)
# Set up references to pytz and dateutil so we know these aren't being used
# If they're called, the widget would try to run None(self.timezone) which
# would raise an exception
clock.pytz = None
clock.dateutil = FakeDateutilTZ
# Pytz timezone must be a string
clk5 = clock.Clock(timezone="1")
fakebar = FakeBar([clk5], window=fake_window)
clk5._configure(fake_qtile, fakebar)
text = clk5.poll()
# Default time is 10:20 and we add 1 hour for the timezone plus and extra
# 1 for the pytz function
assert text == "13:20"
@pytest.mark.usefixtures("patched_clock")
def test_clock_tick(manager_nospawn, minimal_conf_noscreen, monkeypatch):
"""Test clock ticks"""
class FakeDateutilTZ:
class TZ:
@classmethod
def gettz(cls, val):
return int(val) + 2
tz = TZ
class TickingDateTime(datetime.datetime):
offset = 0
@classmethod
def now(cls, *args, **kwargs):
return cls(2021, 1, 1, 10, 20, 30)
# This will return 10:20 on first call and 10:21 on all
# subsequent calls
def astimezone(self, tzone=None):
extra = datetime.timedelta(minutes=TickingDateTime.offset)
if TickingDateTime.offset < 1:
TickingDateTime.offset += 1
if tzone is None:
return self + extra
return self + datetime.timedelta(hours=tzone) + extra
# pytz must not be in the sys.modules dict...
monkeypatch.delitem(sys.modules, "pytz")
# ...but dateutil must be
monkeypatch.setitem(sys.modules, "dateutil", True)
# Override datetime
monkeypatch.setattr("libqtile.widget.clock.datetime", TickingDateTime)
# Set up references to pytz and dateutil so we know these aren't being used
# If they're called, the widget would try to run None(self.timezone) which
# would raise an exception
clock.pytz = None
clock.dateutil = FakeDateutilTZ
# set a long update interval as we'll tick manually
clk6 = clock.Clock(update_interval=100)
config = minimal_conf_noscreen
config.screens = [libqtile.config.Screen(top=libqtile.bar.Bar([clk6], 10))]
manager_nospawn.start(config)
topbar = manager_nospawn.c.bar["top"]
manager_nospawn.c.widget["clock"].eval("self.tick()")
assert topbar.info()["widgets"][0]["text"] == "10:21"
@pytest.mark.usefixtures("patched_clock")
def test_clock_change_timezones(fake_qtile, monkeypatch, fake_window):
"""test commands to change timezones"""
tz1 = datetime.timezone(datetime.timedelta(hours=1))
tz2 = datetime.timezone(-datetime.timedelta(hours=1))
# Pytz timezone must be a string
clk4 = clock.Clock(timezone=tz1)
fakebar = FakeBar([clk4], window=fake_window)
clk4._configure(fake_qtile, fakebar)
text = clk4.poll()
assert text == "11:20"
clk4.update_timezone(tz2)
text = clk4.poll()
assert text == "09:20"
clk4.use_system_timezone()
text = clk4.poll()
assert text == "10:20"
|