test_clock_card.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # -*- coding: utf-8 -*-
  2. import datetime
  3. import mock
  4. import pygame
  5. from pie_time import application as app_module
  6. from pie_time.application import PieTime
  7. from pie_time.cards import ClockCard
  8. class Test_ClockCard(object):
  9. def _dummy_card(self, **settings):
  10. app = PieTime(None, screen_size=(320, 240))
  11. app.path_for_resource = mock.Mock(
  12. side_effect=lambda resource: resource
  13. )
  14. card = ClockCard()
  15. card.set_app(app)
  16. card.set_settings(settings)
  17. return card
  18. def test_initialize(self):
  19. with mock.patch.object(app_module.pygame.font, 'Font',
  20. spec=pygame.font.Font):
  21. card = self._dummy_card()
  22. card.initialize()
  23. assert card._time_font is not None
  24. assert card._date_font is not None
  25. def test_render_time(self):
  26. with mock.patch.object(app_module.pygame.font, 'Font',
  27. spec=pygame.font.Font):
  28. card = self._dummy_card()
  29. card.initialize()
  30. now = datetime.datetime.now().replace(second=0)
  31. card._render_time(now)
  32. card._time_font.render.assert_called_with(
  33. now.strftime(card.TIME_FORMAT), True, card.GREEN
  34. )
  35. now = datetime.datetime.now().replace(second=1)
  36. card._render_time(now)
  37. card._time_font.render.assert_called_with(
  38. now.strftime(card.TIME_FORMAT).replace(':', ' '), True,
  39. card.GREEN
  40. )
  41. def test_render_time_override_format(self):
  42. with mock.patch.object(app_module.pygame.font, 'Font',
  43. spec=pygame.font.Font):
  44. time_format = '%H:%M'
  45. card = self._dummy_card(time_format=time_format)
  46. card.initialize()
  47. now = datetime.datetime.now().replace(second=0)
  48. card._render_time(now)
  49. card._time_font.render.assert_called_with(
  50. now.strftime(time_format), True, card.GREEN
  51. )
  52. def test_render_time_override_color(self):
  53. with mock.patch.object(app_module.pygame.font, 'Font',
  54. spec=pygame.font.Font):
  55. time_color = (255, 255, 255)
  56. card = self._dummy_card(time_color=time_color)
  57. card.initialize()
  58. now = datetime.datetime.now().replace(second=0)
  59. card._render_time(now)
  60. card._time_font.render.assert_called_with(
  61. now.strftime(card.TIME_FORMAT), True, (255, 255, 255)
  62. )
  63. def test_render_time_override_blink(self):
  64. with mock.patch.object(app_module.pygame.font, 'Font',
  65. spec=pygame.font.Font):
  66. card = self._dummy_card(time_blink=False)
  67. card.initialize()
  68. now = datetime.datetime.now().replace(second=1)
  69. card._render_time(now)
  70. card._time_font.render.assert_called_with(
  71. now.strftime(card.TIME_FORMAT), True, card.GREEN
  72. )
  73. def test_render_date(self):
  74. with mock.patch.object(app_module.pygame.font, 'Font',
  75. spec=pygame.font.Font):
  76. card = self._dummy_card()
  77. card.initialize()
  78. now = datetime.datetime.now()
  79. card._render_date(now)
  80. card._date_font.render.assert_called_with(
  81. now.strftime(card.DATE_FORMAT), True, card.GREEN
  82. )
  83. def test_render_date_override_format(self):
  84. with mock.patch.object(app_module.pygame.font, 'Font',
  85. spec=pygame.font.Font):
  86. date_format = '%Y-%M-%D'
  87. card = self._dummy_card(date_format=date_format)
  88. card.initialize()
  89. now = datetime.datetime.now().replace(second=0)
  90. card._render_date(now)
  91. card._date_font.render.assert_called_with(
  92. now.strftime(date_format), True, card.GREEN
  93. )
  94. def test_render_date_override_color(self):
  95. with mock.patch.object(app_module.pygame.font, 'Font',
  96. spec=pygame.font.Font):
  97. date_color = (255, 255, 255)
  98. card = self._dummy_card(date_color=date_color)
  99. card.initialize()
  100. now = datetime.datetime.now().replace(second=0)
  101. card._render_date(now)
  102. card._date_font.render.assert_called_with(
  103. now.strftime(card.DATE_FORMAT), True, (255, 255, 255)
  104. )
  105. def test_update_now(self):
  106. now = datetime.datetime.now()
  107. current_delta = 0
  108. fake_datetime = mock.Mock(spec=datetime.datetime)
  109. def fake_datetime_now(*args, **kwargs):
  110. return now + datetime.timedelta(seconds=current_delta)
  111. fake_datetime.now = mock.Mock(side_effect=fake_datetime_now)
  112. with mock.patch.object(app_module.datetime, 'datetime',
  113. new=fake_datetime):
  114. with mock.patch.object(app_module.pygame.font, 'Font',
  115. spec=pygame.font.Font):
  116. card = self._dummy_card()
  117. card.initialize()
  118. card._app = mock.Mock(spec=PieTime)
  119. card._app._clock = mock.Mock(spec=pygame.time.Clock)
  120. card._app._clock.get_time = mock.Mock(
  121. side_effect=lambda: current_delta
  122. )
  123. assert card._update_now() is True
  124. assert card._now == now
  125. assert card._current_interval == 0
  126. current_delta = 500
  127. assert card._update_now() is False
  128. assert card._now == now
  129. assert card._current_interval == current_delta
  130. current_delta = 530
  131. assert card._update_now() is True
  132. assert card._now > now
  133. assert card._current_interval == 30
  134. def test_show(self):
  135. card = self._dummy_card()
  136. card._now = datetime.datetime.now()
  137. card.show()
  138. assert card._now is None
  139. def test_tick_redraw(self):
  140. now = datetime.datetime.now()
  141. with mock.patch.object(app_module.pygame.font, 'Font',
  142. spec=pygame.font.Font):
  143. card = self._dummy_card()
  144. card.initialize()
  145. card._now = now
  146. card._surface = mock.Mock(spec=pygame.surface.Surface)
  147. card._update_now = mock.Mock(return_value=True)
  148. time_surface = mock.Mock(spec=pygame.surface.Surface)
  149. time_surface.get_size = mock.Mock(return_value=(120, 80))
  150. card._render_time = mock.Mock(return_value=time_surface)
  151. date_surface = mock.Mock(spec=pygame.surface.Surface)
  152. date_surface.get_size = mock.Mock(return_value=(200, 60))
  153. card._render_date = mock.Mock(return_value=date_surface)
  154. card.tick()
  155. assert card._update_now.called
  156. card._render_time.assert_called_with(now)
  157. card._render_date.assert_called_with(now)
  158. assert time_surface.get_size.called
  159. assert date_surface.get_size.called
  160. card.surface.fill.assert_called_with(card.background_color)
  161. card.surface.blit.assert_any_call(
  162. time_surface, (100, 50, 120, 80)
  163. )
  164. card.surface.blit.assert_any_call(
  165. date_surface, (60, 130, 200, 60)
  166. )
  167. def test_tick_dont_redraw(self):
  168. with mock.patch.object(app_module.pygame.font, 'Font',
  169. spec=pygame.font.Font):
  170. card = self._dummy_card()
  171. card.initialize()
  172. card._surface = mock.Mock(spec=pygame.surface.Surface)
  173. card._update_now = mock.Mock(return_value=False)
  174. card._render_time = mock.Mock()
  175. card._render_date = mock.Mock()
  176. card.tick()
  177. assert card._update_now.called
  178. assert card._render_time.called is False
  179. assert card._render_date.called is False
  180. assert card.surface.fill.called is False
  181. assert card.surface.blit.called is False