test_picture_card.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. # -*- coding: utf-8 -*-
  2. import cStringIO
  3. import mock
  4. import pygame
  5. import requests
  6. from pie_time.application import PieTime
  7. from pie_time.cards import picture as card_module
  8. from pie_time.cards import PictureCard
  9. class Test_PictureCard(object):
  10. def _dummy_card(self, **settings):
  11. app = PieTime(None, screen_size=(320, 240))
  12. app.screen = mock.Mock(spec=pygame.surface.Surface)
  13. app._logger = mock.Mock()
  14. app.path_for_resource = mock.Mock(
  15. side_effect=lambda resource: resource
  16. )
  17. card = PictureCard()
  18. card.set_app(app)
  19. card.set_settings(settings)
  20. return card
  21. def _dummy_initialized_card(self, **settings):
  22. card = self._dummy_card(**settings)
  23. card._load_picture = mock.Mock(
  24. side_effect=lambda x: mock.Mock(spec=pygame.surface.Surface)
  25. )
  26. card.initialize()
  27. return card
  28. def test_initialize(self):
  29. urls = ['spam', 'eggs']
  30. card = self._dummy_card(urls=urls)
  31. card._load_picture = mock.Mock()
  32. card.initialize()
  33. assert len(card._pictures) == len(urls)
  34. assert card._current_picture_idx is None
  35. assert card._load_picture.call_count == len(urls)
  36. card._load_picture.assert_any_call(urls[0])
  37. card._load_picture.assert_any_call(urls[1])
  38. def test_load_picture_file(self):
  39. fake_image = mock.Mock(spec=pygame.surface.Surface)
  40. with mock.patch.object(card_module.pygame.image, 'load',
  41. return_value=fake_image):
  42. card = self._dummy_card()
  43. result = card._load_picture('file:///spam')
  44. assert result == fake_image
  45. card_module.pygame.image.load.assert_called_with('/spam')
  46. def test_load_picture_file_load_error(self):
  47. with mock.patch.object(card_module.pygame.image, 'load',
  48. side_effect=RuntimeError('ERROR')):
  49. card = self._dummy_card()
  50. result = card._load_picture('file:///spam')
  51. assert result is None
  52. def test_load_picture_net(self):
  53. fake_image = mock.Mock(spec=pygame.surface.Surface)
  54. fake_response = mock.Mock(spec=requests.Response)
  55. fake_response.status_code = 200
  56. fake_response.headers = {'Content-Type': ''}
  57. fake_response.content = 'HERE IMAGE DATA BE'
  58. fake_stringio = mock.Mock(spec=cStringIO.StringIO)
  59. with mock.patch.object(card_module.pygame.image, 'load',
  60. return_value=fake_image):
  61. with mock.patch.object(card_module.requests, 'get',
  62. return_value=fake_response):
  63. with mock.patch.object(card_module.cStringIO, 'StringIO',
  64. return_value=fake_stringio):
  65. url = 'http://spam.com/eggs'
  66. card = self._dummy_card()
  67. result = card._load_picture(url)
  68. assert result == fake_image
  69. card_module.requests.get.assert_called_with(url)
  70. card_module.cStringIO.StringIO.assert_called_with(
  71. fake_response.content
  72. )
  73. card_module.pygame.image.load.assert_called_with(
  74. fake_stringio, 'picture.'
  75. )
  76. def test_load_picture_net_requests_error(self):
  77. def _get(*args, **kwargs):
  78. raise RuntimeError('ERROR')
  79. new_get = mock.Mock(side_effect=_get)
  80. with mock.patch.object(card_module.requests, 'get',
  81. side_effect=RuntimeError('TODO')):
  82. url = 'http://spam.com/eggs'
  83. card = self._dummy_card()
  84. result = card._load_picture(url)
  85. assert result is None
  86. def test_load_picture_net_bad_response(self):
  87. fake_response = mock.Mock(spec=requests.Response)
  88. fake_response.status_code = 404
  89. with mock.patch.object(card_module.requests, 'get',
  90. return_value=fake_response):
  91. url = 'http://spam.com/eggs'
  92. card = self._dummy_card()
  93. result = card._load_picture(url)
  94. assert result is None
  95. def test_load_picture_net_load_error(self):
  96. fake_response = mock.Mock(spec=requests.Response)
  97. fake_response.status_code = 200
  98. fake_response.headers = {'Content-Type': ''}
  99. fake_response.content = 'HERE IMAGE DATA BE'
  100. with mock.patch.object(card_module.pygame.image, 'load',
  101. side_effect=RuntimeError('ERROR')):
  102. with mock.patch.object(card_module.requests, 'get',
  103. return_value=fake_response):
  104. url = 'http://spam.com/eggs'
  105. card = self._dummy_card()
  106. result = card._load_picture(url)
  107. assert result is None
  108. def test_load_picture_convert(self):
  109. fake_image = mock.Mock(spec=pygame.surface.Surface)
  110. fake_image.convert = mock.Mock(return_value=fake_image)
  111. with mock.patch.object(card_module.pygame.image, 'load',
  112. return_value=fake_image):
  113. card = self._dummy_card()
  114. result = card._load_picture('file:///spam.jpg')
  115. assert result == fake_image
  116. fake_image.convert.assert_called_with(card._app.screen)
  117. def test_load_picture_convert_alpha(self):
  118. fake_image = mock.Mock(spec=pygame.surface.Surface)
  119. fake_image.convert_alpha = mock.Mock(return_value=fake_image)
  120. with mock.patch.object(card_module.pygame.image, 'load',
  121. return_value=fake_image):
  122. card = self._dummy_card()
  123. result = card._load_picture('file:///spam.png')
  124. assert result == fake_image
  125. fake_image.convert_alpha.assert_called_with(card._app.screen)
  126. def test_show_no_pictures(self):
  127. card = self._dummy_initialized_card(urls=[])
  128. card.show()
  129. assert card._current_picture_idx is None
  130. assert card._should_redraw is True
  131. def test_show_one_picture(self):
  132. card = self._dummy_initialized_card(urls=['http://spam.com/eggs.png'])
  133. card.show()
  134. assert card._current_picture_idx == 0
  135. assert card._should_redraw is True
  136. card.show()
  137. assert card._current_picture_idx == 0
  138. assert card._should_redraw is True
  139. def test_show_many_pictures(self):
  140. card = self._dummy_initialized_card(
  141. urls=[
  142. 'http://spam.com/eggs.png', 'http://spam.com/spam.png',
  143. 'http://spam.com/spameggs.png'
  144. ]
  145. )
  146. card.show()
  147. assert card._current_picture_idx == 0
  148. assert card._should_redraw is True
  149. card.show()
  150. assert card._current_picture_idx == 1
  151. assert card._should_redraw is True
  152. card.show()
  153. assert card._current_picture_idx == 2
  154. assert card._should_redraw is True
  155. card.show()
  156. assert card._current_picture_idx == 0
  157. assert card._should_redraw is True
  158. def test_tick_no_pictures(self):
  159. card = self._dummy_initialized_card(urls=[])
  160. card._surface = mock.Mock(spec=pygame.surface.Surface)
  161. card.show()
  162. card.tick()
  163. assert card._should_redraw is False
  164. card.surface.fill.assert_called_with(card.background_color)
  165. def test_tick_with_pictures(self):
  166. card = self._dummy_initialized_card(
  167. urls=['http://spam.com/eggs.png', 'http://spam.com/eggs.png']
  168. )
  169. card._surface = mock.Mock(spec=pygame.surface.Surface)
  170. card._pictures[0].get_size = mock.Mock(return_value=(320, 240))
  171. card._pictures[0].get_rect = mock.Mock(
  172. return_value=(0, 0, 320, 240)
  173. )
  174. card._pictures[1].get_size = mock.Mock(return_value=(240, 180))
  175. card._pictures[1].get_rect = mock.Mock(
  176. return_value=(0, 0, 240, 180)
  177. )
  178. card.show()
  179. card.tick()
  180. assert card._should_redraw is False
  181. card.surface.fill.assert_called_with(card.background_color)
  182. card.surface.blit.assert_called_with(
  183. card._pictures[0], (0, 0, 320, 240)
  184. )
  185. card.show()
  186. card.tick()
  187. assert card._should_redraw is False
  188. card.surface.fill.assert_called_with(card.background_color)
  189. card.surface.blit.assert_called_with(
  190. card._pictures[1], (40, 30, 240, 180)
  191. )
  192. def test_tick_dont_redraw(self):
  193. card = self._dummy_initialized_card(
  194. urls=['http://spam.com/eggs.png', 'http://spam.com/eggs.png']
  195. )
  196. card._surface = mock.Mock(spec=pygame.surface.Surface)
  197. card.show()
  198. card._should_redraw = False
  199. card.tick()
  200. assert card._should_redraw is False
  201. assert card.surface.fill.called is False
  202. assert card.surface.blit.called is False