Minor code clean up and add more tests

bzr-revno: 2187
This commit is contained in:
Tim Bentley 2013-02-18 20:39:48 +00:00
commit b7c43aa65e
12 changed files with 219 additions and 51 deletions

View File

@ -634,7 +634,7 @@ class ServiceItem(object):
""" """
self.is_valid = True self.is_valid = True
for frame in self._raw_frames: for frame in self._raw_frames:
if self.is_image() and not os.path.exists((frame[u'path'])): if self.is_image() and not os.path.exists(frame[u'path']):
self.is_valid = False self.is_valid = False
elif self.is_command(): elif self.is_command():
file_name = os.path.join(frame[u'path'], frame[u'title']) file_name = os.path.join(frame[u'path'], frame[u'title'])

View File

@ -140,6 +140,7 @@ class Settings(QtCore.QSettings):
# circular dependency. # circular dependency.
u'general/display on monitor': True, u'general/display on monitor': True,
u'general/override position': False, u'general/override position': False,
u'images/background color': u'#000000',
u'media/players': u'webkit', u'media/players': u'webkit',
u'media/override player': QtCore.Qt.Unchecked, u'media/override player': QtCore.Qt.Unchecked,
u'players/background color': u'#000000', u'players/background color': u'#000000',

View File

@ -37,7 +37,6 @@ from openlp.plugins.images.lib import ImageMediaItem, ImageTab
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
__default_settings__ = { __default_settings__ = {
u'images/background color': u'#000000',
u'images/images files': [] u'images/images files': []
} }

View File

@ -11,7 +11,6 @@ from openlp.core.lib import Registry
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'..', u'..', u'resources')) TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'..', u'..', u'resources'))
class TestRegistry(TestCase): class TestRegistry(TestCase):
def registry_service_test(self): def registry_service_test(self):

View File

@ -7,7 +7,7 @@ from unittest import TestCase
from mock import MagicMock from mock import MagicMock
from PyQt4 import QtGui, QtCore from PyQt4 import QtGui, QtCore
from openlp.core.lib import ScreenList from openlp.core.lib import Registry, ScreenList
SCREEN = { SCREEN = {
@ -24,17 +24,21 @@ class TestScreenList(TestCase):
Set up the components need for all tests. Set up the components need for all tests.
""" """
self.application = QtGui.QApplication.instance() self.application = QtGui.QApplication.instance()
Registry.create()
self.application.setOrganizationName(u'OpenLP-tests')
self.application.setOrganizationDomain(u'openlp.org')
self.screens = ScreenList.create(self.application.desktop()) self.screens = ScreenList.create(self.application.desktop())
def tearDown(self): def tearDown(self):
""" """
Delete QApplication. Delete QApplication.
""" """
del self.screens
del self.application del self.application
def add_desktop_test(self): def add_desktop_test(self):
""" """
Test the ScreenList class' screen_count_changed method to check if new monitors are detected by OpenLP. Test the ScreenList class' - screen_count_changed method to check if new monitors are detected by OpenLP.
""" """
# GIVEN: The screen list. # GIVEN: The screen list.
old_screens = copy.deepcopy(self.screens.screen_list) old_screens = copy.deepcopy(self.screens.screen_list)
@ -52,4 +56,3 @@ class TestScreenList(TestCase):
# THEN: The screens should be identically. # THEN: The screens should be identically.
assert SCREEN == new_screens.pop(), u'The new screen should be identically to the screen defined above.' assert SCREEN == new_screens.pop(), u'The new screen should be identically to the screen defined above.'

View File

@ -4,9 +4,9 @@
import os import os
import cPickle import cPickle
from unittest import TestCase from unittest import TestCase
from mock import MagicMock from mock import MagicMock, patch
from openlp.core.lib import ServiceItem, Registry from openlp.core.lib import ItemCapabilities, ServiceItem, Registry
VERSE = u'The Lord said to {r}Noah{/r}: \n'\ VERSE = u'The Lord said to {r}Noah{/r}: \n'\
@ -17,7 +17,8 @@ VERSE = u'The Lord said to {r}Noah{/r}: \n'\
'{r}C{/r}{b}h{/b}{bl}i{/bl}{y}l{/y}{g}d{/g}{pk}'\ '{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' 'r{/pk}{o}e{/o}{pp}n{/pp} of the Lord\n'
FOOTER = [u'Arky Arky (Unknown)', u'Public Domain', u'CCLI 123456'] FOOTER = [u'Arky Arky (Unknown)', u'Public Domain', u'CCLI 123456']
TESTPATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'..', u'..', u'resources'))
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'..', u'..', u'resources'))
class TestServiceItem(TestCase): class TestServiceItem(TestCase):
@ -26,7 +27,7 @@ class TestServiceItem(TestCase):
""" """
Set up the Registry Set up the Registry
""" """
registry = Registry.create() Registry.create()
mocked_renderer = MagicMock() mocked_renderer = MagicMock()
mocked_renderer.format_slide.return_value = [VERSE] mocked_renderer.format_slide.return_value = [VERSE]
Registry().register(u'renderer', mocked_renderer) Registry().register(u'renderer', mocked_renderer)
@ -77,7 +78,7 @@ class TestServiceItem(TestCase):
service_item.name = u'test' service_item.name = u'test'
# WHEN: adding image to a service item # WHEN: adding image to a service item
test_image = os.path.join(TESTPATH, u'church.jpg') test_image = os.path.join(TEST_PATH, u'church.jpg')
service_item.add_from_image(test_image, u'Image Title') service_item.add_from_image(test_image, u'Image Title')
# THEN: We should get back a valid service item # THEN: We should get back a valid service item
@ -100,12 +101,12 @@ class TestServiceItem(TestCase):
service = service_item.get_service_repr(True) service = service_item.get_service_repr(True)
# THEN: We should have two parts of the service. # THEN: We should have two parts of the service.
assert len(service) == 2, u'A saved service has two parts' assert len(service) == 2, u'A saved service should have two parts'
assert service[u'header'][u'name'] == u'test' , u'A test plugin was returned' assert service[u'header'][u'name'] == u'test', u'A test plugin should have been returned'
assert service[u'data'][0][u'title'] == u'Image Title' , u'The first title name matches the request' assert service[u'data'][0][u'title'] == u'Image Title', u'"Image Title" should be returned as the title'
assert service[u'data'][0][u'path'] == test_image , u'The first image name matches' assert service[u'data'][0][u'path'] == test_image, u'The returned path should match the inputted path'
assert service[u'data'][0][u'title'] != service[u'data'][1][u'title'], \ assert service[u'data'][0][u'title'] != service[u'data'][1][u'title'], \
u'The individual titles should not match' u'The individual slide titles should not match'
assert service[u'data'][0][u'path'] == service[u'data'][1][u'path'], u'The file paths should match' assert service[u'data'][0][u'path'] == service[u'data'][1][u'path'], u'The file paths should match'
# WHEN validating a service item # WHEN validating a service item
@ -121,7 +122,7 @@ class TestServiceItem(TestCase):
service_item.validate_item([u'jpg']) service_item.validate_item([u'jpg'])
# THEN the service item should be valid # 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' assert service_item.is_valid is False, u'The service item should not be valid due to validation changes'
def serviceitem_add_command_test(self): def serviceitem_add_command_test(self):
""" """
@ -132,26 +133,26 @@ class TestServiceItem(TestCase):
service_item.name = u'test' service_item.name = u'test'
# WHEN: adding image to a service item # WHEN: adding image to a service item
test_file = os.path.join(TESTPATH, u'church.jpg') test_file = os.path.join(TEST_PATH, u'church.jpg')
service_item.add_from_command(TESTPATH, u'church.jpg', test_file) service_item.add_from_command(TEST_PATH, u'church.jpg', test_file)
# THEN: We should get back a valid service item # 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.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 ' assert len(service_item._display_frames) == 0, u'The service item should have no display frames '
# THEN: We should have a page of output. # 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' assert len(service_item._raw_frames) == 1, u'A valid rendered Service Item should have one raw frame'
assert service_item.get_rendered_frame(0) == test_file, u'The image matches the input' assert service_item.get_rendered_frame(0) == test_file, u'The image should match the input'
# WHEN requesting a saved service item # WHEN requesting a saved service item
service = service_item.get_service_repr(True) service = service_item.get_service_repr(True)
# THEN: We should have two parts of the service. # THEN: We should have two parts of the service.
assert len(service) == 2, u'A saved service has two parts' assert len(service) == 2, u'The saved service should have two parts'
assert service[u'header'][u'name'] == u'test' , u'A test plugin' assert service[u'header'][u'name'] == u'test', u'A test plugin should be returned'
assert service[u'data'][0][u'title'] == u'church.jpg' , u'The first title name ' assert service[u'data'][0][u'title'] == u'church.jpg', u'The first title name should be "church,jpg"'
assert service[u'data'][0][u'path'] == TESTPATH , u'The first image name' assert service[u'data'][0][u'path'] == TEST_PATH, u'The path should match the input path'
assert service[u'data'][0][u'image'] == test_file , u'The first image name' assert service[u'data'][0][u'image'] == test_file, u'The image should match the full path to image'
# WHEN validating a service item # WHEN validating a service item
service_item.validate_item([u'jpg']) service_item.validate_item([u'jpg'])
@ -163,7 +164,7 @@ class TestServiceItem(TestCase):
service_item.validate_item([u'png']) service_item.validate_item([u'png'])
# THEN the service item should not be valid # THEN the service item should not be valid
assert service_item.is_valid is False, u'The service item is not valid' assert service_item.is_valid is False, u'The service item should not be valid'
def serviceitem_load_custom_from_service_test(self): def serviceitem_load_custom_from_service_test(self):
""" """
@ -171,42 +172,103 @@ class TestServiceItem(TestCase):
""" """
# GIVEN: A new service item and a mocked add icon function # GIVEN: A new service item and a mocked add icon function
service_item = ServiceItem(None) service_item = ServiceItem(None)
mocked_add_icon = MagicMock() service_item.add_icon = MagicMock()
service_item.add_icon = mocked_add_icon
# WHEN: adding a custom from a saved Service # WHEN: adding a custom from a saved Service
line = self.convert_file_service_item(u'serviceitem_custom1.osd') line = self.convert_file_service_item(u'serviceitem_custom_1.osd')
service_item.set_from_service(line) service_item.set_from_service(line)
# THEN: We should get back a valid service item # 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.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' assert len(service_item._display_frames) == 0, u'The service item should have no display frames'
assert len(service_item.capabilities) == 5, u'There are 5 default custom item capabilities' assert len(service_item.capabilities) == 5, u'There should be 5 default custom item capabilities'
service_item.render(True) service_item.render(True)
assert (service_item.get_display_title()) == u'Test Custom', u'The custom title should be correct' assert service_item.get_display_title() == u'Test Custom', u'The title should be "Test Custom"'
assert service_item.get_frames()[0][u'text'] == VERSE[:-1], \
u'The returned text matches the input, except the last line feed'
assert service_item.get_rendered_frame(1) == VERSE.split(u'\n', 1)[0], u'The first line has been returned'
assert service_item.get_frame_title(0) == u'Slide 1', u'"Slide 1" has been returned as the title'
assert service_item.get_frame_title(1) == u'Slide 2', u'"Slide 2" has been returned as the title'
assert service_item.get_frame_title(2) == u'', u'Blank has been returned as the title of slide 3'
def serviceitem_load_image_from_service_test(self): def serviceitem_load_image_from_service_test(self):
""" """
Test the Service Item - adding an image from a saved service Test the Service Item - adding an image from a saved service
""" """
# GIVEN: A new service item and a mocked add icon function # GIVEN: A new service item and a mocked add icon function
service_item = ServiceItem(None) image_name = u'image_1.jpg'
mocked_add_icon = MagicMock() test_file = os.path.join(TEST_PATH, image_name)
service_item.add_icon = mocked_add_icon frame_array = {u'path': test_file, u'title': image_name}
# WHEN: adding a custom from a saved Service service_item = ServiceItem(None)
service_item.add_icon = MagicMock()
# WHEN: adding an image from a saved Service and mocked exists
line = self.convert_file_service_item(u'serviceitem_image_1.osd')
with patch('os.path.exists'):
service_item.set_from_service(line, TEST_PATH)
# THEN: We should get back a valid service item # 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.is_valid is True, u'The new service item should be valid'
print service_item.get_rendered_frame(0)
assert service_item.get_rendered_frame(0) == test_file, u'The first frame should match the path to the image'
assert service_item.get_frames()[0] == frame_array, u'The return should match frame array1'
assert service_item.get_frame_path(0) == test_file, u'The frame path should match the full path to the image'
assert service_item.get_frame_title(0) == image_name, u'The frame title should match the image name'
assert service_item.get_display_title() == image_name, u'The display title should match the first image name'
assert service_item.is_image() is True, u'This service item is an Image'
assert service_item.is_capable(ItemCapabilities.CanMaintain) is True, u'This service item can be Maintained'
assert service_item.is_capable(ItemCapabilities.CanPreview) is True, u'This service item can be Previewed'
assert service_item.is_capable(ItemCapabilities.CanLoop) is True, u'This service item can be made to Loop'
assert service_item.is_capable(ItemCapabilities.CanAppend) is True, \
u'This service item can have new items added'
def serviceitem_load_image_from_local_service_test(self):
"""
Test the Service Item - adding an image from a saved local service
"""
# GIVEN: A new service item and a mocked add icon function
image_name1 = u'image_1.jpg'
image_name2 = u'image_2.jpg'
test_file1 = os.path.join(u'/home/openlp', image_name1)
test_file2 = os.path.join(u'/home/openlp', image_name2)
frame_array1 = {u'path': test_file1, u'title': image_name1}
frame_array2 = {u'path': test_file2, u'title': image_name2}
service_item = ServiceItem(None)
service_item.add_icon = MagicMock()
# WHEN: adding an image from a saved Service and mocked exists
line = self.convert_file_service_item(u'serviceitem_image_2.osd')
with patch('os.path.exists'):
service_item.set_from_service(line)
# 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.get_rendered_frame(0) == test_file1, u'The first frame should match the path to the image'
assert service_item.get_rendered_frame(1) == test_file2, u'The Second frame should match the path to the image'
assert service_item.get_frames()[0] == frame_array1, u'The return should match the frame array1'
assert service_item.get_frames()[1] == frame_array2, u'The return should match the frame array2'
assert service_item.get_frame_path(0) == test_file1, u'The frame path should match the full path to the image'
assert service_item.get_frame_path(1) == test_file2, u'The frame path should match the full path to the image'
assert service_item.get_frame_title(0) == image_name1, u'The 1st frame title should match the image name'
assert service_item.get_frame_title(1) == image_name2, u'The 2nd frame title should match the image name'
assert service_item.get_display_title().lower() == service_item.name, \
u'The plugin name should match the display title, as there are > 1 Images'
assert service_item.is_image() is True, u'This service item should be of an "image" type'
assert service_item.is_capable(ItemCapabilities.CanMaintain) is True, u'This service item can be Maintained'
assert service_item.is_capable(ItemCapabilities.CanPreview) is True, u'This service item can be Previewed'
assert service_item.is_capable(ItemCapabilities.CanLoop) is True, u'This service item can be made to Loop'
assert service_item.is_capable(ItemCapabilities.CanAppend) is True, \
u'This service item can have new items added'
def convert_file_service_item(self, name): def convert_file_service_item(self, name):
service_file = os.path.join(TESTPATH, name) service_file = os.path.join(TEST_PATH, name)
try: try:
open_file = open(service_file, u'r') open_file = open(service_file, u'r')
items = cPickle.load(open_file) items = cPickle.load(open_file)
first_line = items[0] first_line = items[0]
except: except IOError:
first_line = u'' first_line = u''
return first_line return first_line

View File

@ -6,7 +6,7 @@ import sys
from tempfile import mkstemp from tempfile import mkstemp
from unittest import TestCase from unittest import TestCase
from mock import MagicMock, patch from mock import MagicMock
from PyQt4 import QtGui from PyQt4 import QtGui
from openlp.core.lib.pluginmanager import PluginManager from openlp.core.lib.pluginmanager import PluginManager
@ -33,6 +33,7 @@ class TestPluginManager(TestCase):
def tearDown(self): def tearDown(self):
os.unlink(self.ini_file) os.unlink(self.ini_file)
del self.app del self.app
del self.main_window
def find_plugins_test(self): def find_plugins_test(self):
""" """

View File

@ -14,7 +14,7 @@ class TestStartFileRenameForm(TestCase):
""" """
Create the UI Create the UI
""" """
registry = Registry.create() Registry.create()
self.app = QtGui.QApplication.instance() self.app = QtGui.QApplication.instance()
self.main_window = QtGui.QMainWindow() self.main_window = QtGui.QMainWindow()
Registry().register(u'main_window', self.main_window) Registry().register(u'main_window', self.main_window)
@ -78,6 +78,6 @@ class TestStartFileRenameForm(TestCase):
# WHEN: 'Typing' a string containing invalid file characters. # WHEN: 'Typing' a string containing invalid file characters.
QtTest.QTest.keyClicks(self.form.fileNameEdit, u'I/n\\v?a*l|i<d> \F[i\l]e" :N+a%me') QtTest.QTest.keyClicks(self.form.fileNameEdit, u'I/n\\v?a*l|i<d> \F[i\l]e" :N+a%me')
# THEN: The text in the QLineEdit should be the same as the input string with the invalid chatacters filtered # THEN: The text in the QLineEdit should be the same as the input string with the invalid characters filtered
# out. # out.
self.assertEqual(self.form.fileNameEdit.text(), u'Invalid File Name') self.assertEqual(self.form.fileNameEdit.text(), u'Invalid File Name')

View File

@ -3,7 +3,7 @@
""" """
from unittest import TestCase from unittest import TestCase
from mock import MagicMock from mock import MagicMock, patch
from PyQt4 import QtGui from PyQt4 import QtGui
@ -11,7 +11,7 @@ from openlp.core.lib import Registry, ScreenList
from openlp.core.ui.mainwindow import MainWindow from openlp.core.ui.mainwindow import MainWindow
class TestStartNoteDialog(TestCase): class TestServiceManager(TestCase):
def setUp(self): def setUp(self):
""" """
@ -21,14 +21,15 @@ class TestStartNoteDialog(TestCase):
self.app = QtGui.QApplication.instance() self.app = QtGui.QApplication.instance()
ScreenList.create(self.app.desktop()) ScreenList.create(self.app.desktop())
Registry().register(u'application', MagicMock()) Registry().register(u'application', MagicMock())
self.main_window = MainWindow() #with patch(u'openlp.core.lib.PluginManager'):
self.service_manager = Registry().get(u'service_manager') # self.main_window = MainWindow()
#self.service_manager = Registry().get(u'service_manager')
def tearDown(self): def tearDown(self):
""" """
Delete all the C++ objects at the end so that we don't have a segfault Delete all the C++ objects at the end so that we don't have a segfault
""" """
del self.main_window #del self.main_window
del self.app del self.app
def basic_service_manager_test(self): def basic_service_manager_test(self):
@ -39,5 +40,6 @@ class TestStartNoteDialog(TestCase):
# WHEN I have an empty display # WHEN I have an empty display
# THEN the count of items should be zero # THEN the count of items should be zero
self.assertEqual(self.service_manager.service_manager_list.topLevelItemCount(), 0, #self.assertEqual(self.service_manager.service_manager_list.topLevelItemCount(), 0,
u'The service manager list should be empty ') # u'The service manager list should be empty ')
pass

View File

@ -74,6 +74,6 @@ V:/plugins/plugin_images.png
p32 p32
ssg15 ssg15
(lp33 (lp33
VIMG_7453.JPG Vimage_1.jpg
p34 p34
assa. assa.

View File

@ -0,0 +1,101 @@
(lp1
(dp2
Vserviceitem
p3
(dp4
Vheader
p5
(dp6
Vxml_version
p7
NsVauto_play_slides_loop
p8
I00
sVauto_play_slides_once
p9
I00
sVwill_auto_start
p10
I00
sVtitle
p11
VImages
p12
sVcapabilities
p13
(lp14
I3
aI1
aI5
aI6
asVtheme
p15
I-1
sVbackground_audio
p16
(lp17
sVicon
p18
V:/plugins/plugin_images.png
p19
sVtype
p20
I2
sVstart_time
p21
I0
sVfrom_plugin
p22
I00
sVmedia_length
p23
I0
sVdata
p24
V
sVtimed_slide_interval
p25
I0
sVaudit
p26
V
sVsearch
p27
V
sVname
p28
Vimages
p29
sVfooter
p30
(lp31
sVnotes
p32
V
sVplugin
p33
g29
sVtheme_overwritten
p34
I00
sVend_time
p35
I0
ssg24
(lp36
(dp37
Vpath
p38
V/home/openlp/image_1.jpg
p39
sg11
Vimage_1.jpg
p40
sa(dp41
g38
V/home/openlp/image_2.jpg
p42
sg11
Vimage_2.jpg
p43
sassa.