openlp/tests/functional/openlp_plugins/images/test_lib.py

202 lines
9.9 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
2014-12-31 10:58:13 +00:00
# Copyright (c) 2008-2015 Raoul Snyman #
# Portions copyright (c) 2008-2015 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 #
###############################################################################
"""
This module contains tests for the lib submodule of the Images plugin.
"""
from unittest import TestCase
2013-12-13 17:44:05 +00:00
from openlp.core.common import Registry
from openlp.plugins.images.lib.db import ImageFilenames, ImageGroups
from openlp.plugins.images.lib.mediaitem import ImageMediaItem
from tests.functional import MagicMock, patch
class TestImageMediaItem(TestCase):
"""
This is a test case to test various methods in the ImageMediaItem class.
"""
def setUp(self):
self.mocked_main_window = MagicMock()
Registry.create()
2013-08-31 18:17:38 +00:00
Registry().register('service_list', MagicMock())
Registry().register('main_window', self.mocked_main_window)
Registry().register('live_controller', MagicMock())
mocked_plugin = MagicMock()
with patch('openlp.plugins.images.lib.mediaitem.MediaManagerItem._setup'), \
patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.setup_item'):
self.media_item = ImageMediaItem(None, mocked_plugin)
def save_new_images_list_empty_list_test(self):
"""
Test that the save_new_images_list() method handles empty lists gracefully
"""
# GIVEN: An empty image_list
image_list = []
with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list'):
2013-03-19 20:05:13 +00:00
self.media_item.manager = MagicMock()
# WHEN: We run save_new_images_list with the empty list
2013-03-19 20:05:13 +00:00
self.media_item.save_new_images_list(image_list)
# THEN: The save_object() method should not have been called
2013-03-19 20:05:13 +00:00
assert self.media_item.manager.save_object.call_count == 0, \
2013-08-31 18:17:38 +00:00
'The save_object() method should not have been called'
def save_new_images_list_single_image_with_reload_test(self):
"""
2013-04-19 19:07:25 +00:00
Test that the save_new_images_list() calls load_full_list() when reload_list is set to True
"""
# GIVEN: A list with 1 image and a mocked out manager
image_list = ['test_image.jpg']
2013-08-31 18:17:38 +00:00
with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list') as mocked_load_full_list:
ImageFilenames.filename = ''
2013-03-19 20:05:13 +00:00
self.media_item.manager = MagicMock()
# WHEN: We run save_new_images_list with reload_list=True
2013-03-19 20:05:13 +00:00
self.media_item.save_new_images_list(image_list, reload_list=True)
2013-04-19 19:07:25 +00:00
# THEN: load_full_list() should have been called
2013-08-31 18:17:38 +00:00
assert mocked_load_full_list.call_count == 1, 'load_full_list() should have been called'
# CLEANUP: Remove added attribute from ImageFilenames
delattr(ImageFilenames, 'filename')
def save_new_images_list_single_image_without_reload_test(self):
"""
2013-04-19 19:07:25 +00:00
Test that the save_new_images_list() doesn't call load_full_list() when reload_list is set to False
"""
# GIVEN: A list with 1 image and a mocked out manager
image_list = ['test_image.jpg']
2013-08-31 18:17:38 +00:00
with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list') as mocked_load_full_list:
2013-03-19 20:05:13 +00:00
self.media_item.manager = MagicMock()
# WHEN: We run save_new_images_list with reload_list=False
2013-03-19 20:05:13 +00:00
self.media_item.save_new_images_list(image_list, reload_list=False)
2013-04-19 19:07:25 +00:00
# THEN: load_full_list() should not have been called
2013-08-31 18:17:38 +00:00
assert mocked_load_full_list.call_count == 0, 'load_full_list() should not have been called'
def save_new_images_list_multiple_images_test(self):
"""
Test that the save_new_images_list() saves all images in the list
"""
# GIVEN: A list with 3 images
2014-04-02 18:51:21 +00:00
image_list = ['test_image_1.jpg', 'test_image_2.jpg', 'test_image_3.jpg']
2013-08-31 18:17:38 +00:00
with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list') as mocked_load_full_list:
2013-03-19 20:05:13 +00:00
self.media_item.manager = MagicMock()
# WHEN: We run save_new_images_list with the list of 3 images
2013-03-19 20:05:13 +00:00
self.media_item.save_new_images_list(image_list, reload_list=False)
2013-04-19 19:07:25 +00:00
# THEN: load_full_list() should not have been called
2013-03-19 20:05:13 +00:00
assert self.media_item.manager.save_object.call_count == 3, \
2013-08-31 18:17:38 +00:00
'load_full_list() should have been called three times'
def save_new_images_list_other_objects_in_list_test(self):
"""
Test that the save_new_images_list() ignores everything in the provided list except strings
"""
# GIVEN: A list with images and objects
2014-04-02 18:51:21 +00:00
image_list = ['test_image_1.jpg', None, True, ImageFilenames(), 'test_image_2.jpg']
2013-08-31 18:17:38 +00:00
with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list') as mocked_load_full_list:
2013-03-19 20:05:13 +00:00
self.media_item.manager = MagicMock()
# WHEN: We run save_new_images_list with the list of images and objects
2013-03-19 20:05:13 +00:00
self.media_item.save_new_images_list(image_list, reload_list=False)
2013-04-19 19:07:25 +00:00
# THEN: load_full_list() should not have been called
2013-03-19 20:05:13 +00:00
assert self.media_item.manager.save_object.call_count == 2, \
2013-08-31 18:17:38 +00:00
'load_full_list() should have been called only once'
2013-03-29 12:00:06 +00:00
def on_reset_click_test(self):
"""
2013-05-14 14:57:37 +00:00
Test that on_reset_click() actually resets the background
2013-03-29 12:00:06 +00:00
"""
2013-05-14 14:57:37 +00:00
# GIVEN: A mocked version of reset_action
self.media_item.reset_action = MagicMock()
2013-03-29 12:00:06 +00:00
2013-05-14 14:57:37 +00:00
# WHEN: on_reset_click is called
self.media_item.on_reset_click()
2013-03-29 12:00:06 +00:00
2013-05-14 14:57:37 +00:00
# THEN: the reset_action should be set visible, and the image should be reset
self.media_item.reset_action.setVisible.assert_called_with(False)
2013-03-29 12:00:06 +00:00
self.media_item.live_controller.display.reset_image.assert_called_with()
def recursively_delete_group_test(self):
"""
Test that recursively_delete_group() works
"""
# GIVEN: An ImageGroups object and mocked functions
2013-08-31 18:17:38 +00:00
with patch('openlp.core.utils.delete_file') as mocked_delete_file:
ImageFilenames.group_id = 1
ImageGroups.parent_id = 1
self.media_item.manager = MagicMock()
2013-05-19 11:31:02 +00:00
self.media_item.manager.get_all_objects.side_effect = self._recursively_delete_group_side_effect
self.media_item.service_path = ""
test_group = ImageGroups()
test_group.id = 1
# WHEN: recursively_delete_group() is called
self.media_item.recursively_delete_group(test_group)
# THEN:
2013-08-31 18:17:38 +00:00
assert mocked_delete_file.call_count == 0, 'delete_file() should not be called'
assert self.media_item.manager.delete_object.call_count == 7, \
2013-08-31 18:17:38 +00:00
'manager.delete_object() should be called exactly 7 times'
2014-04-02 18:51:21 +00:00
# CLEANUP: Remove added attribute from Image Filenames and ImageGroups
delattr(ImageFilenames, 'group_id')
delattr(ImageGroups, 'parent_id')
def _recursively_delete_group_side_effect(*args, **kwargs):
"""
Side effect method that creates custom return values for the recursively_delete_group method
"""
if args[1] == ImageFilenames and args[2]:
# Create some fake objects that should be removed
returned_object1 = ImageFilenames()
returned_object1.id = 1
returned_object1.filename = '/tmp/test_file_1.jpg'
returned_object2 = ImageFilenames()
returned_object2.id = 2
returned_object2.filename = '/tmp/test_file_2.jpg'
returned_object3 = ImageFilenames()
returned_object3.id = 3
returned_object3.filename = '/tmp/test_file_3.jpg'
return [returned_object1, returned_object2, returned_object3]
if args[1] == ImageGroups and args[2]:
# Change the parent_id that is matched so we don't get into an endless loop
ImageGroups.parent_id = 0
# Create a fake group that will be used in the next run
returned_object1 = ImageGroups()
returned_object1.id = 1
return [returned_object1]
return []