267 lines
8.8 KiB
Python
267 lines
8.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import cStringIO
|
|
|
|
import mock
|
|
import pygame
|
|
import requests
|
|
|
|
from pie_time.application import PieTime
|
|
from pie_time.cards import picture as card_module
|
|
from pie_time.cards import PictureCard
|
|
|
|
|
|
class Test_PictureCard(object):
|
|
def _dummy_card(self, **settings):
|
|
app = PieTime(None, screen_size=(320, 240))
|
|
app.screen = mock.Mock(spec=pygame.surface.Surface)
|
|
app._logger = mock.Mock()
|
|
|
|
app.path_for_resource = mock.Mock(
|
|
side_effect=lambda resource: resource
|
|
)
|
|
|
|
card = PictureCard()
|
|
card.set_app(app)
|
|
card.set_settings(settings)
|
|
|
|
return card
|
|
|
|
def _dummy_initialized_card(self, **settings):
|
|
card = self._dummy_card(**settings)
|
|
card._load_picture = mock.Mock(
|
|
side_effect=lambda x: mock.Mock(spec=pygame.surface.Surface)
|
|
)
|
|
|
|
card.initialize()
|
|
return card
|
|
|
|
def test_initialize(self):
|
|
urls = ['spam', 'eggs']
|
|
|
|
card = self._dummy_card(urls=urls)
|
|
card._load_picture = mock.Mock()
|
|
|
|
card.initialize()
|
|
assert len(card._pictures) == len(urls)
|
|
assert card._current_picture_idx is None
|
|
|
|
assert card._load_picture.call_count == len(urls)
|
|
card._load_picture.assert_any_call(urls[0])
|
|
card._load_picture.assert_any_call(urls[1])
|
|
|
|
def test_load_picture_file(self):
|
|
fake_image = mock.Mock(spec=pygame.surface.Surface)
|
|
|
|
with mock.patch.object(card_module.pygame.image, 'load',
|
|
return_value=fake_image):
|
|
card = self._dummy_card()
|
|
result = card._load_picture('file:///spam')
|
|
assert result == fake_image
|
|
|
|
card_module.pygame.image.load.assert_called_with('/spam')
|
|
|
|
def test_load_picture_file_load_error(self):
|
|
with mock.patch.object(card_module.pygame.image, 'load',
|
|
side_effect=RuntimeError('ERROR')):
|
|
card = self._dummy_card()
|
|
result = card._load_picture('file:///spam')
|
|
assert result is None
|
|
|
|
def test_load_picture_net(self):
|
|
fake_image = mock.Mock(spec=pygame.surface.Surface)
|
|
|
|
fake_response = mock.Mock(spec=requests.Response)
|
|
fake_response.status_code = 200
|
|
fake_response.headers = {'Content-Type': ''}
|
|
fake_response.content = 'HERE IMAGE DATA BE'
|
|
|
|
fake_stringio = mock.Mock(spec=cStringIO.StringIO)
|
|
|
|
with mock.patch.object(card_module.pygame.image, 'load',
|
|
return_value=fake_image):
|
|
with mock.patch.object(card_module.requests, 'get',
|
|
return_value=fake_response):
|
|
with mock.patch.object(card_module.cStringIO, 'StringIO',
|
|
return_value=fake_stringio):
|
|
url = 'http://spam.com/eggs'
|
|
card = self._dummy_card()
|
|
|
|
result = card._load_picture(url)
|
|
assert result == fake_image
|
|
|
|
card_module.requests.get.assert_called_with(url)
|
|
|
|
card_module.cStringIO.StringIO.assert_called_with(
|
|
fake_response.content
|
|
)
|
|
|
|
card_module.pygame.image.load.assert_called_with(
|
|
fake_stringio, 'picture.'
|
|
)
|
|
|
|
def test_load_picture_net_requests_error(self):
|
|
def _get(*args, **kwargs):
|
|
raise RuntimeError('ERROR')
|
|
|
|
new_get = mock.Mock(side_effect=_get)
|
|
|
|
with mock.patch.object(card_module.requests, 'get',
|
|
side_effect=RuntimeError('TODO')):
|
|
url = 'http://spam.com/eggs'
|
|
card = self._dummy_card()
|
|
|
|
result = card._load_picture(url)
|
|
assert result is None
|
|
|
|
def test_load_picture_net_bad_response(self):
|
|
fake_response = mock.Mock(spec=requests.Response)
|
|
fake_response.status_code = 404
|
|
|
|
with mock.patch.object(card_module.requests, 'get',
|
|
return_value=fake_response):
|
|
url = 'http://spam.com/eggs'
|
|
card = self._dummy_card()
|
|
|
|
result = card._load_picture(url)
|
|
assert result is None
|
|
|
|
def test_load_picture_net_load_error(self):
|
|
fake_response = mock.Mock(spec=requests.Response)
|
|
fake_response.status_code = 200
|
|
fake_response.headers = {'Content-Type': ''}
|
|
fake_response.content = 'HERE IMAGE DATA BE'
|
|
|
|
with mock.patch.object(card_module.pygame.image, 'load',
|
|
side_effect=RuntimeError('ERROR')):
|
|
with mock.patch.object(card_module.requests, 'get',
|
|
return_value=fake_response):
|
|
url = 'http://spam.com/eggs'
|
|
card = self._dummy_card()
|
|
|
|
result = card._load_picture(url)
|
|
assert result is None
|
|
|
|
def test_load_picture_convert(self):
|
|
fake_image = mock.Mock(spec=pygame.surface.Surface)
|
|
fake_image.convert = mock.Mock(return_value=fake_image)
|
|
|
|
with mock.patch.object(card_module.pygame.image, 'load',
|
|
return_value=fake_image):
|
|
card = self._dummy_card()
|
|
result = card._load_picture('file:///spam.jpg')
|
|
assert result == fake_image
|
|
|
|
fake_image.convert.assert_called_with(card._app.screen)
|
|
|
|
def test_load_picture_convert_alpha(self):
|
|
fake_image = mock.Mock(spec=pygame.surface.Surface)
|
|
fake_image.convert_alpha = mock.Mock(return_value=fake_image)
|
|
|
|
with mock.patch.object(card_module.pygame.image, 'load',
|
|
return_value=fake_image):
|
|
card = self._dummy_card()
|
|
result = card._load_picture('file:///spam.png')
|
|
assert result == fake_image
|
|
|
|
fake_image.convert_alpha.assert_called_with(card._app.screen)
|
|
|
|
def test_show_no_pictures(self):
|
|
card = self._dummy_initialized_card(urls=[])
|
|
card.show()
|
|
assert card._current_picture_idx is None
|
|
assert card._should_redraw is True
|
|
|
|
def test_show_one_picture(self):
|
|
card = self._dummy_initialized_card(urls=['http://spam.com/eggs.png'])
|
|
|
|
card.show()
|
|
assert card._current_picture_idx == 0
|
|
assert card._should_redraw is True
|
|
|
|
card.show()
|
|
assert card._current_picture_idx == 0
|
|
assert card._should_redraw is True
|
|
|
|
def test_show_many_pictures(self):
|
|
card = self._dummy_initialized_card(
|
|
urls=[
|
|
'http://spam.com/eggs.png', 'http://spam.com/spam.png',
|
|
'http://spam.com/spameggs.png'
|
|
]
|
|
)
|
|
|
|
card.show()
|
|
assert card._current_picture_idx == 0
|
|
assert card._should_redraw is True
|
|
|
|
card.show()
|
|
assert card._current_picture_idx == 1
|
|
assert card._should_redraw is True
|
|
|
|
card.show()
|
|
assert card._current_picture_idx == 2
|
|
assert card._should_redraw is True
|
|
|
|
card.show()
|
|
assert card._current_picture_idx == 0
|
|
assert card._should_redraw is True
|
|
|
|
def test_tick_no_pictures(self):
|
|
card = self._dummy_initialized_card(urls=[])
|
|
card._surface = mock.Mock(spec=pygame.surface.Surface)
|
|
card.show()
|
|
|
|
card.tick()
|
|
assert card._should_redraw is False
|
|
|
|
card.surface.fill.assert_called_with(card.background_color)
|
|
|
|
def test_tick_with_pictures(self):
|
|
card = self._dummy_initialized_card(
|
|
urls=['http://spam.com/eggs.png', 'http://spam.com/eggs.png']
|
|
)
|
|
|
|
card._surface = mock.Mock(spec=pygame.surface.Surface)
|
|
|
|
card._pictures[0].get_size = mock.Mock(return_value=(320, 240))
|
|
card._pictures[0].get_rect = mock.Mock(
|
|
return_value=(0, 0, 320, 240)
|
|
)
|
|
|
|
card._pictures[1].get_size = mock.Mock(return_value=(240, 180))
|
|
card._pictures[1].get_rect = mock.Mock(
|
|
return_value=(0, 0, 240, 180)
|
|
)
|
|
|
|
card.show()
|
|
card.tick()
|
|
assert card._should_redraw is False
|
|
card.surface.fill.assert_called_with(card.background_color)
|
|
card.surface.blit.assert_called_with(
|
|
card._pictures[0], (0, 0, 320, 240)
|
|
)
|
|
|
|
card.show()
|
|
card.tick()
|
|
assert card._should_redraw is False
|
|
card.surface.fill.assert_called_with(card.background_color)
|
|
card.surface.blit.assert_called_with(
|
|
card._pictures[1], (40, 30, 240, 180)
|
|
)
|
|
|
|
def test_tick_dont_redraw(self):
|
|
card = self._dummy_initialized_card(
|
|
urls=['http://spam.com/eggs.png', 'http://spam.com/eggs.png']
|
|
)
|
|
|
|
card._surface = mock.Mock(spec=pygame.surface.Surface)
|
|
card.show()
|
|
card._should_redraw = False
|
|
|
|
card.tick()
|
|
assert card._should_redraw is False
|
|
|
|
assert card.surface.fill.called is False
|
|
assert card.surface.blit.called is False
|