2013-08-25 18:31:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-09-19 21:02:28 +00:00
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
2013-08-25 18:31:15 +00:00
|
|
|
|
2013-09-19 21:02:28 +00:00
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# Copyright (c) 2008-2013 Raoul Snyman #
|
|
|
|
# Portions copyright (c) 2008-2013 Tim Bentley, Gerald Britton, Jonathan #
|
|
|
|
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
|
|
|
|
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
|
|
|
|
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
|
|
|
|
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
|
|
|
|
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
|
|
|
|
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# This program is free software; you can redistribute it and/or modify it #
|
|
|
|
# under the terms of the GNU General Public License as published by the Free #
|
|
|
|
# Software Foundation; version 2 of the License. #
|
|
|
|
# #
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT #
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
|
|
|
|
# more details. #
|
|
|
|
# #
|
|
|
|
# You should have received a copy of the GNU General Public License along #
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
|
|
|
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
|
|
|
###############################################################################
|
2012-12-08 08:50:21 +00:00
|
|
|
"""
|
2013-09-19 21:02:28 +00:00
|
|
|
Package to test the openlp.core.lib package.
|
2012-12-08 08:50:21 +00:00
|
|
|
"""
|
2013-01-19 20:46:11 +00:00
|
|
|
import os
|
2012-12-08 08:50:21 +00:00
|
|
|
from unittest import TestCase
|
2013-02-16 06:51:25 +00:00
|
|
|
|
2013-09-19 21:02:28 +00:00
|
|
|
from tests.functional import MagicMock, patch
|
2013-09-22 21:11:03 +00:00
|
|
|
from tests.utils import assert_length, convert_file_service_item
|
2012-12-08 11:21:57 +00:00
|
|
|
|
2013-10-28 03:40:38 +00:00
|
|
|
from openlp.core.lib import ItemCapabilities, ServiceItem, Registry, \
|
|
|
|
ServiceItemType
|
2013-10-11 10:13:04 +00:00
|
|
|
|
2012-12-08 11:21:57 +00:00
|
|
|
|
2013-08-31 18:17:38 +00:00
|
|
|
VERSE = 'The Lord said to {r}Noah{/r}: \n'\
|
2012-12-08 11:21:57 +00:00
|
|
|
'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'
|
2013-08-31 18:17:38 +00:00
|
|
|
FOOTER = ['Arky Arky (Unknown)', 'Public Domain', 'CCLI 123456']
|
2013-11-01 15:03:43 +00:00
|
|
|
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'resources'))
|
2013-08-25 18:31:15 +00:00
|
|
|
|
2013-01-20 10:01:51 +00:00
|
|
|
|
2012-12-08 08:50:21 +00:00
|
|
|
class TestServiceItem(TestCase):
|
|
|
|
|
2013-01-22 21:59:45 +00:00
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Set up the Registry
|
|
|
|
"""
|
2013-02-16 18:24:31 +00:00
|
|
|
Registry.create()
|
2013-01-27 20:59:02 +00:00
|
|
|
mocked_renderer = MagicMock()
|
2013-01-22 21:59:45 +00:00
|
|
|
mocked_renderer.format_slide.return_value = [VERSE]
|
2013-08-31 18:17:38 +00:00
|
|
|
Registry().register('renderer', mocked_renderer)
|
|
|
|
Registry().register('image_manager', MagicMock())
|
2013-01-22 21:59:45 +00:00
|
|
|
|
2013-09-22 21:11:03 +00:00
|
|
|
def service_item_basic_test(self):
|
2012-12-08 08:50:21 +00:00
|
|
|
"""
|
2013-01-20 10:01:51 +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-02-16 20:00:31 +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
|
2013-11-01 15:03:43 +00:00
|
|
|
self.assertTrue(service_item.is_valid, 'The new service item should be valid')
|
|
|
|
self.assertTrue(service_item.missing_frames(), 'There should not be any frames in the service item')
|
2012-12-08 11:21:57 +00:00
|
|
|
|
2013-09-22 21:11:03 +00:00
|
|
|
def service_item_load_custom_from_service_test(self):
|
2013-01-20 10:01:51 +00:00
|
|
|
"""
|
2013-01-20 17:12:33 +00:00
|
|
|
Test the Service Item - adding a custom slide from a saved service
|
2013-01-20 10:01:51 +00:00
|
|
|
"""
|
2013-08-25 18:31:15 +00:00
|
|
|
# GIVEN: A new service item and a mocked add icon function
|
2013-01-20 10:01:51 +00:00
|
|
|
service_item = ServiceItem(None)
|
2013-08-25 18:31:15 +00:00
|
|
|
service_item.add_icon = MagicMock()
|
2013-01-24 19:38:38 +00:00
|
|
|
|
2013-09-22 21:11:03 +00:00
|
|
|
# WHEN: We add a custom from a saved service
|
2013-09-29 21:28:53 +00:00
|
|
|
line = convert_file_service_item(TEST_PATH, 'serviceitem_custom_1.osj')
|
2013-08-25 18:31:15 +00:00
|
|
|
service_item.set_from_service(line)
|
2013-01-20 10:01:51 +00:00
|
|
|
|
|
|
|
# THEN: We should get back a valid service item
|
2013-11-01 15:03:43 +00:00
|
|
|
self.assertTrue(service_item.is_valid, 'The new service item should be valid')
|
|
|
|
assert_length(0, service_item._display_frames, 'The service item should have no display frames')
|
|
|
|
assert_length(5, service_item.capabilities, 'There should be 5 default custom item capabilities')
|
2013-09-22 21:11:03 +00:00
|
|
|
|
|
|
|
# WHEN: We render the frames of the service item
|
2013-01-20 10:01:51 +00:00
|
|
|
service_item.render(True)
|
2013-09-22 21:11:03 +00:00
|
|
|
|
|
|
|
# THEN: The frames should also be valid
|
2013-11-01 15:03:43 +00:00
|
|
|
self.assertEqual('Test Custom', service_item.get_display_title(), 'The title should be "Test Custom"')
|
2013-09-22 21:11:03 +00:00
|
|
|
self.assertEqual(VERSE[:-1], service_item.get_frames()[0]['text'],
|
2013-10-02 21:07:20 +00:00
|
|
|
'The returned text matches the input, except the last line feed')
|
2013-11-01 15:03:43 +00:00
|
|
|
self.assertEqual(VERSE.split('\n', 1)[0], service_item.get_rendered_frame(1),
|
2013-10-02 21:07:20 +00:00
|
|
|
'The first line has been returned')
|
2013-11-01 15:03:43 +00:00
|
|
|
self.assertEqual('Slide 1', service_item.get_frame_title(0), '"Slide 1" has been returned as the title')
|
|
|
|
self.assertEqual('Slide 2', service_item.get_frame_title(1), '"Slide 2" has been returned as the title')
|
|
|
|
self.assertEqual('', service_item.get_frame_title(2), 'Blank has been returned as the title of slide 3')
|
2013-09-22 21:11:03 +00:00
|
|
|
|
|
|
|
def service_item_load_image_from_service_test(self):
|
2013-01-20 17:12:33 +00:00
|
|
|
"""
|
|
|
|
Test the Service Item - adding an image from a saved service
|
|
|
|
"""
|
2013-08-25 18:31:15 +00:00
|
|
|
# GIVEN: A new service item and a mocked add icon function
|
2013-08-31 18:17:38 +00:00
|
|
|
image_name = 'image_1.jpg'
|
2013-08-25 18:31:15 +00:00
|
|
|
test_file = os.path.join(TEST_PATH, image_name)
|
2013-08-31 18:17:38 +00:00
|
|
|
frame_array = {'path': test_file, 'title': image_name}
|
2013-02-16 18:24:31 +00:00
|
|
|
|
2013-01-20 17:12:33 +00:00
|
|
|
service_item = ServiceItem(None)
|
2013-08-25 18:31:15 +00:00
|
|
|
service_item.add_icon = MagicMock()
|
2013-01-20 17:12:33 +00:00
|
|
|
|
2013-02-16 18:24:31 +00:00
|
|
|
# WHEN: adding an image from a saved Service and mocked exists
|
2013-09-22 21:11:03 +00:00
|
|
|
line = convert_file_service_item(TEST_PATH, 'serviceitem_image_1.osj')
|
2013-11-01 15:03:43 +00:00
|
|
|
with patch('openlp.core.ui.servicemanager.os.path.exists') as mocked_exists:
|
2013-08-25 18:31:15 +00:00
|
|
|
mocked_exists.return_value = True
|
|
|
|
service_item.set_from_service(line, TEST_PATH)
|
2013-01-20 17:12:33 +00:00
|
|
|
|
2013-10-28 02:33:28 +00:00
|
|
|
# THEN: We should get back a valid service item
|
2013-11-01 15:03:43 +00:00
|
|
|
self.assertTrue(service_item.is_valid, 'The new service item should be valid')
|
2013-10-28 02:33:28 +00:00
|
|
|
self.assertEqual(os.path.normpath(test_file),
|
|
|
|
os.path.normpath(service_item.get_rendered_frame(0)),
|
|
|
|
'The first frame should match the path to the image')
|
|
|
|
self.assertEqual(frame_array, service_item.get_frames()[0],
|
|
|
|
'The return should match frame array1')
|
|
|
|
self.assertEqual(test_file, service_item.get_frame_path(0),
|
|
|
|
'The frame path should match the full path to the image')
|
|
|
|
self.assertEqual(image_name, service_item.get_frame_title(0),
|
|
|
|
'The frame title should match the image name')
|
|
|
|
self.assertEqual(image_name, service_item.get_display_title(),
|
|
|
|
'The display title should match the first image name')
|
2013-11-01 15:03:43 +00:00
|
|
|
self.assertTrue(service_item.is_image(), 'This service item should be of an "image" type')
|
2013-10-28 02:33:28 +00:00
|
|
|
self.assertTrue(service_item.is_capable(ItemCapabilities.CanMaintain),
|
|
|
|
'This service item should be able to be Maintained')
|
|
|
|
self.assertTrue(service_item.is_capable(ItemCapabilities.CanPreview),
|
|
|
|
'This service item should be able to be be Previewed')
|
|
|
|
self.assertTrue(service_item.is_capable(ItemCapabilities.CanLoop),
|
|
|
|
'This service item should be able to be run in a can be made to Loop')
|
|
|
|
self.assertTrue(service_item.is_capable(ItemCapabilities.CanAppend),
|
|
|
|
'This service item should be able to have new items added to it')
|
2013-09-22 21:11:03 +00:00
|
|
|
|
|
|
|
def service_item_load_image_from_local_service_test(self):
|
2013-02-16 20:04:26 +00:00
|
|
|
"""
|
|
|
|
Test the Service Item - adding an image from a saved local service
|
|
|
|
"""
|
2013-08-25 18:31:15 +00:00
|
|
|
# GIVEN: A new service item and a mocked add icon function
|
2013-08-31 18:17:38 +00:00
|
|
|
image_name1 = 'image_1.jpg'
|
|
|
|
image_name2 = 'image_2.jpg'
|
2013-10-28 02:33:28 +00:00
|
|
|
test_file1 = os.path.normpath(os.path.join('/home/openlp',
|
|
|
|
image_name1))
|
|
|
|
test_file2 = os.path.normpath(os.path.join('/home/openlp',
|
|
|
|
image_name2))
|
2013-08-31 18:17:38 +00:00
|
|
|
frame_array1 = {'path': test_file1, 'title': image_name1}
|
|
|
|
frame_array2 = {'path': test_file2, 'title': image_name2}
|
2013-02-16 20:04:26 +00:00
|
|
|
|
|
|
|
service_item = ServiceItem(None)
|
2013-08-25 18:31:15 +00:00
|
|
|
service_item.add_icon = MagicMock()
|
|
|
|
|
|
|
|
service_item2 = ServiceItem(None)
|
|
|
|
service_item2.add_icon = MagicMock()
|
2013-02-16 20:04:26 +00:00
|
|
|
|
|
|
|
# WHEN: adding an image from a saved Service and mocked exists
|
2013-09-22 21:11:03 +00:00
|
|
|
line = convert_file_service_item(TEST_PATH, 'serviceitem_image_2.osj')
|
|
|
|
line2 = convert_file_service_item(TEST_PATH, 'serviceitem_image_2.osj', 1)
|
2013-08-25 18:31:15 +00:00
|
|
|
|
2013-11-01 15:03:43 +00:00
|
|
|
with patch('openlp.core.ui.servicemanager.os.path.exists') as mocked_exists:
|
2013-08-25 18:31:15 +00:00
|
|
|
mocked_exists.return_value = True
|
|
|
|
service_item2.set_from_service(line2)
|
|
|
|
service_item.set_from_service(line)
|
|
|
|
|
2013-10-28 02:33:28 +00:00
|
|
|
# THEN: We should get back a valid service item
|
2013-08-25 18:31:15 +00:00
|
|
|
|
2013-11-01 15:03:43 +00:00
|
|
|
# This test is copied from service_item.py, but is changed since to conform to
|
|
|
|
# new layout of service item. The layout use in serviceitem_image_2.osd is actually invalid now.
|
|
|
|
self.assertTrue(service_item.is_valid, 'The first service item should be valid')
|
|
|
|
self.assertTrue(service_item2.is_valid, 'The second service item should be valid')
|
2013-10-28 02:33:28 +00:00
|
|
|
self.assertEqual(test_file1,
|
|
|
|
os.path.normpath(service_item.get_rendered_frame(0)),
|
|
|
|
'The first frame should match the path to the image')
|
|
|
|
self.assertEqual(test_file2,
|
|
|
|
os.path.normpath(service_item2.get_rendered_frame(0)),
|
|
|
|
'The Second frame should match the path to the image')
|
|
|
|
# There is a problem with the following two asserts in Windows
|
|
|
|
# and it is not easily fixable (although it looks simple)
|
|
|
|
if os.name != 'nt':
|
2013-11-01 15:03:43 +00:00
|
|
|
self.assertEqual(frame_array1, service_item.get_frames()[0], 'The return should match the frame array1')
|
|
|
|
self.assertEqual(frame_array2, service_item2.get_frames()[0], 'The return should match the frame array2')
|
2013-10-28 02:33:28 +00:00
|
|
|
self.assertEqual(test_file1, os.path.normpath(
|
|
|
|
service_item.get_frame_path(0)),
|
|
|
|
'The frame path should match the full path to the image')
|
|
|
|
self.assertEqual(test_file2, os.path.normpath(
|
|
|
|
service_item2.get_frame_path(0)),
|
|
|
|
'The frame path should match the full path to the image')
|
|
|
|
self.assertEqual(image_name1, service_item.get_frame_title(0),
|
|
|
|
'The 1st frame title should match the image name')
|
|
|
|
self.assertEqual(image_name2, service_item2.get_frame_title(0),
|
|
|
|
'The 2nd frame title should match the image name')
|
|
|
|
self.assertEqual(service_item.name, service_item.title.lower(),
|
|
|
|
'The plugin name should match the display title, as there are > 1 Images')
|
2013-11-01 15:03:43 +00:00
|
|
|
self.assertTrue(service_item.is_image(), 'This service item should be of an "image" type')
|
2013-10-28 02:33:28 +00:00
|
|
|
self.assertTrue(service_item.is_capable(ItemCapabilities.CanMaintain),
|
|
|
|
'This service item should be able to be Maintained')
|
|
|
|
self.assertTrue(service_item.is_capable(ItemCapabilities.CanPreview),
|
|
|
|
'This service item should be able to be be Previewed')
|
|
|
|
self.assertTrue(service_item.is_capable(ItemCapabilities.CanLoop),
|
|
|
|
'This service item should be able to be run in a can be made to Loop')
|
|
|
|
self.assertTrue(service_item.is_capable(ItemCapabilities.CanAppend),
|
|
|
|
'This service item should be able to have new items added to it')
|
2013-10-25 17:58:55 +00:00
|
|
|
|
|
|
|
def add_from_command_for_a_presentation_test(self):
|
|
|
|
"""
|
|
|
|
Test the Service Item - adding a presentation
|
|
|
|
"""
|
|
|
|
# GIVEN: A service item, a mocked icon and presentation data
|
|
|
|
service_item = ServiceItem(None)
|
|
|
|
presentation_name = 'test.pptx'
|
|
|
|
image = MagicMock()
|
2013-10-28 02:33:28 +00:00
|
|
|
display_title = 'DisplayTitle'
|
2013-10-25 17:58:55 +00:00
|
|
|
notes = 'Note1\nNote2\n'
|
|
|
|
frame = {'title': presentation_name, 'image': image,
|
2013-10-28 02:33:28 +00:00
|
|
|
'path': TEST_PATH, 'display_title': display_title,
|
2013-10-25 17:58:55 +00:00
|
|
|
'notes': notes }
|
|
|
|
# WHEN: adding presentation to service_item
|
2013-10-28 02:33:28 +00:00
|
|
|
service_item.add_from_command(TEST_PATH, presentation_name, image, display_title, notes)
|
2013-10-25 17:58:55 +00:00
|
|
|
# THEN: verify that it is setup as a Command and that the frame data matches
|
|
|
|
assert service_item.service_item_type == ServiceItemType.Command
|
|
|
|
assert service_item.get_frames()[0] == frame
|
|
|
|
|
2013-10-28 02:33:28 +00:00
|
|
|
def add_from_comamnd_without_display_title_and_notes_test(self):
|
2013-10-25 17:58:55 +00:00
|
|
|
"""
|
|
|
|
Test the Service Item - add from command, but not presentation
|
|
|
|
"""
|
|
|
|
# GIVEN: A new service item, a mocked icon and image data
|
|
|
|
service_item = ServiceItem(None)
|
|
|
|
image_name = 'test.img'
|
|
|
|
image = MagicMock()
|
|
|
|
frame = {'title': image_name, 'image': image,
|
2013-10-28 02:33:28 +00:00
|
|
|
'path': TEST_PATH, 'display_title': None,
|
2013-10-25 17:58:55 +00:00
|
|
|
'notes': None}
|
|
|
|
# WHEN: adding image to service_item
|
|
|
|
service_item.add_from_command(TEST_PATH, image_name, image)
|
|
|
|
# THEN: verify that it is setup as a Command and that the frame data matches
|
|
|
|
assert service_item.service_item_type == ServiceItemType.Command
|
|
|
|
print(service_item.get_frames()[0])
|
|
|
|
assert service_item.get_frames()[0] == frame
|