openlp/tests/functional/openlp_core_lib/test_serviceitem.py

163 lines
7.0 KiB
Python
Raw Normal View History

2012-12-08 08:50:21 +00:00
"""
Package to test the openlp.core.lib package.
"""
2013-01-19 20:46:11 +00:00
import os
2012-12-08 08:50:21 +00:00
from unittest import TestCase
2012-12-30 08:33:42 +00:00
from mock import MagicMock
2012-12-09 20:57:41 +00:00
from openlp.core.lib import ServiceItem
2012-12-08 11:21:57 +00:00
VERSE = u'The Lord said to {r}Noah{/r}: \n'\
'There\'s gonna be a {su}floody{/su}, {sb}floody{/sb}\n'\
'The Lord said to {g}Noah{/g}:\n'\
'There\'s gonna be a {st}floody{/st}, {it}floody{/it}\n'\
'Get those children out of the muddy, muddy \n'\
'{r}C{/r}{b}h{/b}{bl}i{/bl}{y}l{/y}{g}d{/g}{pk}'\
'r{/pk}{o}e{/o}{pp}n{/pp} of the Lord\n'
FOOTER = [u'Arky Arky (Unknown)', u'Public Domain', u'CCLI 123456']
2012-12-08 08:50:21 +00:00
2013-01-19 21:34:16 +00:00
TESTPATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'resources'))
2013-01-19 20:46:11 +00:00
2012-12-08 08:50:21 +00:00
class TestServiceItem(TestCase):
def serviceitem_basic_test(self):
"""
2013-01-02 21:50:20 +00:00
Test the Service Item basic test
2012-12-08 08:50:21 +00:00
"""
2013-01-19 18:31:22 +00:00
# GIVEN: A new service item
2012-12-09 20:57:41 +00:00
2013-01-19 18:31:22 +00:00
# WHEN:A service item is created (without a plugin)
2012-12-09 20:57:41 +00:00
service_item = ServiceItem(None)
2013-01-19 18:31:22 +00:00
# THEN: We should get back a valid service item
assert service_item.is_valid is True, u'The new service item should be valid'
assert service_item.missing_frames() is True, u'There should not be any frames in the service item'
2012-12-08 11:21:57 +00:00
def serviceitem_add_text_test(self):
"""
2013-01-02 21:50:20 +00:00
Test the Service Item add text test
2012-12-08 11:21:57 +00:00
"""
2013-01-19 18:31:22 +00:00
# GIVEN: A new service item
2012-12-09 20:57:41 +00:00
service_item = ServiceItem(None)
2013-01-19 18:31:22 +00:00
# WHEN: adding text to a service item
2012-12-09 20:57:41 +00:00
service_item.add_from_text(VERSE)
service_item.raw_footer = FOOTER
2013-01-19 18:31:22 +00:00
# THEN: We should get back a valid service item
assert service_item.is_valid is True, u'The new service item should be valid'
2013-01-02 21:50:20 +00:00
assert service_item.missing_frames() is False, u'check frames loaded '
2012-12-09 20:57:41 +00:00
2013-01-19 18:31:22 +00:00
# GIVEN: A service item with text
2012-12-09 20:57:41 +00:00
mocked_renderer = MagicMock()
mocked_renderer.format_slide.return_value = [VERSE]
service_item.renderer = mocked_renderer
2013-01-19 18:31:22 +00:00
# WHEN: Render called
assert len(service_item._display_frames) == 0, u'A blank Service Item with no display frames'
2012-12-09 20:57:41 +00:00
service_item.render(True)
2013-01-19 18:31:22 +00:00
# THEN: We should have a page of output.
assert len(service_item._display_frames) == 1, u'A valid rendered Service Item has 1 display frame'
assert service_item.get_rendered_frame(0) == VERSE.split(u'\n')[0], u'A output has rendered correctly.'
2012-12-30 19:03:10 +00:00
2012-12-30 22:10:51 +00:00
def serviceitem_add_image_test(self):
2012-12-30 19:03:10 +00:00
"""
2013-01-02 21:50:20 +00:00
Test the Service Item add image test
2012-12-30 19:03:10 +00:00
"""
2013-01-19 18:31:22 +00:00
# GIVEN: A new service item and a mocked renderer
2012-12-30 19:03:10 +00:00
service_item = ServiceItem(None)
service_item.name = u'test'
mocked_renderer = MagicMock()
service_item.renderer = mocked_renderer
2013-01-19 18:31:22 +00:00
# WHEN: adding image to a service item
2013-01-19 20:46:11 +00:00
test_image = os.path.join(TESTPATH, u'church.jpg')
service_item.add_from_image(test_image, u'Image Title')
2012-12-30 19:03:10 +00:00
2013-01-19 18:31:22 +00:00
# THEN: We should get back a valid service item
assert service_item.is_valid is True, u'The new service item should be valid'
assert len(service_item._display_frames) == 0, u'The service item has no display frames'
2012-12-30 19:03:10 +00:00
2013-01-19 18:31:22 +00:00
# THEN: We should have a page of output.
assert len(service_item._raw_frames) == 1, u'A valid rendered Service Item has display frames'
2013-01-19 20:46:11 +00:00
assert service_item.get_rendered_frame(0) == test_image
2012-12-30 19:03:10 +00:00
2013-01-19 18:31:22 +00:00
# WHEN: adding a second image to a service item
2013-01-19 20:46:11 +00:00
service_item.add_from_image(test_image, u'Image1 Title')
2012-12-30 19:03:10 +00:00
2013-01-19 18:31:22 +00:00
# THEN: We should have an increased page of output.
assert len(service_item._raw_frames) == 2, u'A valid rendered Service Item has display frames'
2013-01-19 20:46:11 +00:00
assert service_item.get_rendered_frame(0) == test_image
2012-12-30 19:03:10 +00:00
assert service_item.get_rendered_frame(0) == service_item.get_rendered_frame(1)
2013-01-19 18:31:22 +00:00
# WHEN requesting a saved service item
2012-12-30 19:03:10 +00:00
service = service_item.get_service_repr(True)
2013-01-19 18:31:22 +00:00
# THEN: We should have two parts of the service.
assert len(service) == 2, u'A saved service has two parts'
assert service[u'header'][u'name'] == u'test' , u'A test plugin was returned'
assert service[u'data'][0][u'title'] == u'Image Title' , u'The first title name matches the request'
2013-01-19 20:46:11 +00:00
assert service[u'data'][0][u'path'] == test_image , u'The first image name matches'
2013-01-19 18:31:22 +00:00
assert service[u'data'][0][u'title'] != service[u'data'][1][u'title'], \
u'The individual titles should not match'
assert service[u'data'][0][u'path'] == service[u'data'][1][u'path'], u'The file paths should match'
2012-12-30 22:10:51 +00:00
2013-01-19 18:31:22 +00:00
# WHEN validating a service item
2012-12-30 22:10:51 +00:00
service_item.validate_item([u'jpg'])
2013-01-19 18:31:22 +00:00
# THEN the service item should be valid
assert service_item.is_valid is True, u'The new service item should be valid'
2012-12-30 22:10:51 +00:00
2013-01-02 21:50:20 +00:00
# WHEN: adding a second image to a service item
service_item.add_from_image(u'resources/church1.jpg', u'Image1 Title')
2013-01-19 18:31:22 +00:00
# WHEN validating a service item
2013-01-02 21:50:20 +00:00
service_item.validate_item([u'jpg'])
2013-01-19 18:31:22 +00:00
# THEN the service item should be valid
assert service_item.is_valid is False, u'The service item is not valid due to validation changes'
2013-01-02 21:50:20 +00:00
2012-12-30 22:10:51 +00:00
def serviceitem_add_command_test(self):
"""
2013-01-02 21:50:20 +00:00
Test the Service Item add command test
2012-12-30 22:10:51 +00:00
"""
2013-01-19 18:31:22 +00:00
# GIVEN: A new service item and a mocked renderer
2012-12-30 22:10:51 +00:00
service_item = ServiceItem(None)
service_item.name = u'test'
mocked_renderer = MagicMock()
service_item.renderer = mocked_renderer
2013-01-19 18:31:22 +00:00
# WHEN: adding image to a service item
2013-01-19 20:46:11 +00:00
test_file = os.path.join(TESTPATH, u'church.jpg')
service_item.add_from_command(TESTPATH, u'church.jpg', test_file)
2012-12-30 22:10:51 +00:00
2013-01-19 18:31:22 +00:00
# THEN: We should get back a valid service item
assert service_item.is_valid is True, u'The new service item should be valid'
assert len(service_item._display_frames) == 0, u'The service item has no display frames '
2012-12-30 22:10:51 +00:00
2013-01-19 18:31:22 +00:00
# THEN: We should have a page of output.
assert len(service_item._raw_frames) == 1, u'A valid rendered Service Item has one raw frame'
2013-01-19 20:46:11 +00:00
assert service_item.get_rendered_frame(0) == test_file, u'The image matches the input'
2012-12-30 22:10:51 +00:00
2013-01-19 18:31:22 +00:00
# WHEN requesting a saved service item
2012-12-30 22:10:51 +00:00
service = service_item.get_service_repr(True)
2013-01-19 18:31:22 +00:00
# THEN: We should have two parts of the service.
assert len(service) == 2, u'A saved service has two parts'
2012-12-30 22:10:51 +00:00
assert service[u'header'][u'name'] == u'test' , u'A test plugin'
assert service[u'data'][0][u'title'] == u'church.jpg' , u'The first title name '
2013-01-19 20:46:11 +00:00
assert service[u'data'][0][u'path'] == TESTPATH , u'The first image name'
assert service[u'data'][0][u'image'] == test_file , u'The first image name'
2012-12-30 22:10:51 +00:00
2013-01-19 18:31:22 +00:00
# WHEN validating a service item
2012-12-30 22:10:51 +00:00
service_item.validate_item([u'jpg'])
2013-01-19 18:31:22 +00:00
# THEN the service item should be valid
2013-01-02 21:50:20 +00:00
assert service_item.is_valid is True, u'The service item is valid'
2013-01-19 18:31:22 +00:00
# WHEN validating a service item with a different suffix
2013-01-02 21:50:20 +00:00
service_item.validate_item([u'png'])
2013-01-19 18:31:22 +00:00
# THEN the service item should not be valid
2013-01-02 21:50:20 +00:00
assert service_item.is_valid is False, u'The service item is not valid'