pie-time/tests/test_clock_card.py

230 lines
8.1 KiB
Python

# -*- coding: utf-8 -*-
import datetime
import mock
import pygame
from pie_time import application as app_module
from pie_time.application import PieTime
from pie_time.cards import ClockCard
class Test_ClockCard(object):
def _dummy_card(self, **settings):
app = PieTime(None, screen_size=(320, 240))
app.path_for_resource = mock.Mock(
side_effect=lambda resource: resource
)
card = ClockCard()
card.set_app(app)
card.set_settings(settings)
return card
def test_initialize(self):
with mock.patch.object(app_module.pygame.font, 'Font',
spec=pygame.font.Font):
card = self._dummy_card()
card.initialize()
assert card._time_font is not None
assert card._date_font is not None
def test_render_time(self):
with mock.patch.object(app_module.pygame.font, 'Font',
spec=pygame.font.Font):
card = self._dummy_card()
card.initialize()
now = datetime.datetime.now().replace(second=0)
card._render_time(now)
card._time_font.render.assert_called_with(
now.strftime(card.TIME_FORMAT), True, card.GREEN
)
now = datetime.datetime.now().replace(second=1)
card._render_time(now)
card._time_font.render.assert_called_with(
now.strftime(card.TIME_FORMAT).replace(':', ' '), True,
card.GREEN
)
def test_render_time_override_format(self):
with mock.patch.object(app_module.pygame.font, 'Font',
spec=pygame.font.Font):
time_format = '%H:%M'
card = self._dummy_card(time_format=time_format)
card.initialize()
now = datetime.datetime.now().replace(second=0)
card._render_time(now)
card._time_font.render.assert_called_with(
now.strftime(time_format), True, card.GREEN
)
def test_render_time_override_color(self):
with mock.patch.object(app_module.pygame.font, 'Font',
spec=pygame.font.Font):
time_color = (255, 255, 255)
card = self._dummy_card(time_color=time_color)
card.initialize()
now = datetime.datetime.now().replace(second=0)
card._render_time(now)
card._time_font.render.assert_called_with(
now.strftime(card.TIME_FORMAT), True, (255, 255, 255)
)
def test_render_time_override_blink(self):
with mock.patch.object(app_module.pygame.font, 'Font',
spec=pygame.font.Font):
card = self._dummy_card(time_blink=False)
card.initialize()
now = datetime.datetime.now().replace(second=1)
card._render_time(now)
card._time_font.render.assert_called_with(
now.strftime(card.TIME_FORMAT), True, card.GREEN
)
def test_render_date(self):
with mock.patch.object(app_module.pygame.font, 'Font',
spec=pygame.font.Font):
card = self._dummy_card()
card.initialize()
now = datetime.datetime.now()
card._render_date(now)
card._date_font.render.assert_called_with(
now.strftime(card.DATE_FORMAT), True, card.GREEN
)
def test_render_date_override_format(self):
with mock.patch.object(app_module.pygame.font, 'Font',
spec=pygame.font.Font):
date_format = '%Y-%M-%D'
card = self._dummy_card(date_format=date_format)
card.initialize()
now = datetime.datetime.now().replace(second=0)
card._render_date(now)
card._date_font.render.assert_called_with(
now.strftime(date_format), True, card.GREEN
)
def test_render_date_override_color(self):
with mock.patch.object(app_module.pygame.font, 'Font',
spec=pygame.font.Font):
date_color = (255, 255, 255)
card = self._dummy_card(date_color=date_color)
card.initialize()
now = datetime.datetime.now().replace(second=0)
card._render_date(now)
card._date_font.render.assert_called_with(
now.strftime(card.DATE_FORMAT), True, (255, 255, 255)
)
def test_update_now(self):
now = datetime.datetime.now()
current_delta = 0
fake_datetime = mock.Mock(spec=datetime.datetime)
def fake_datetime_now(*args, **kwargs):
return now + datetime.timedelta(seconds=current_delta)
fake_datetime.now = mock.Mock(side_effect=fake_datetime_now)
with mock.patch.object(app_module.datetime, 'datetime',
new=fake_datetime):
with mock.patch.object(app_module.pygame.font, 'Font',
spec=pygame.font.Font):
card = self._dummy_card()
card.initialize()
card._app = mock.Mock(spec=PieTime)
card._app._clock = mock.Mock(spec=pygame.time.Clock)
card._app._clock.get_time = mock.Mock(
side_effect=lambda: current_delta
)
assert card._update_now() is True
assert card._now == now
assert card._current_interval == 0
current_delta = 500
assert card._update_now() is False
assert card._now == now
assert card._current_interval == current_delta
current_delta = 530
assert card._update_now() is True
assert card._now > now
assert card._current_interval == 30
def test_show(self):
card = self._dummy_card()
card._now = datetime.datetime.now()
card.show()
assert card._now is None
def test_tick_redraw(self):
now = datetime.datetime.now()
with mock.patch.object(app_module.pygame.font, 'Font',
spec=pygame.font.Font):
card = self._dummy_card()
card.initialize()
card._now = now
card._surface = mock.Mock(spec=pygame.surface.Surface)
card._update_now = mock.Mock(return_value=True)
time_surface = mock.Mock(spec=pygame.surface.Surface)
time_surface.get_size = mock.Mock(return_value=(120, 80))
card._render_time = mock.Mock(return_value=time_surface)
date_surface = mock.Mock(spec=pygame.surface.Surface)
date_surface.get_size = mock.Mock(return_value=(200, 60))
card._render_date = mock.Mock(return_value=date_surface)
card.tick()
assert card._update_now.called
card._render_time.assert_called_with(now)
card._render_date.assert_called_with(now)
assert time_surface.get_size.called
assert date_surface.get_size.called
card.surface.fill.assert_called_with(card.background_color)
card.surface.blit.assert_any_call(
time_surface, (100, 50, 120, 80)
)
card.surface.blit.assert_any_call(
date_surface, (60, 130, 200, 60)
)
def test_tick_dont_redraw(self):
with mock.patch.object(app_module.pygame.font, 'Font',
spec=pygame.font.Font):
card = self._dummy_card()
card.initialize()
card._surface = mock.Mock(spec=pygame.surface.Surface)
card._update_now = mock.Mock(return_value=False)
card._render_time = mock.Mock()
card._render_date = mock.Mock()
card.tick()
assert card._update_now.called
assert card._render_time.called is False
assert card._render_date.called is False
assert card.surface.fill.called is False
assert card.surface.blit.called is False