2013-03-18 22:04:09 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
2013-09-19 21:02:28 +00:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2016-12-31 11:01:36 +00:00
|
|
|
# Copyright (c) 2008-2017 OpenLP Developers #
|
2013-09-19 21:02:28 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# 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 #
|
|
|
|
###############################################################################
|
2013-03-18 22:04:09 +00:00
|
|
|
"""
|
|
|
|
This module contains tests for the lib submodule of the Images plugin.
|
|
|
|
"""
|
|
|
|
from unittest import TestCase
|
2017-05-08 19:04:14 +00:00
|
|
|
from unittest.mock import ANY, MagicMock, patch
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-11-07 00:49:40 +00:00
|
|
|
from PyQt5 import QtCore, QtWidgets
|
2015-05-17 21:13:05 +00:00
|
|
|
|
2017-10-07 07:05:07 +00:00
|
|
|
from openlp.core.common.registry import Registry
|
2017-08-26 15:06:11 +00:00
|
|
|
from openlp.core.common.path import Path
|
2013-03-29 14:15:16 +00:00
|
|
|
from openlp.plugins.images.lib.db import ImageFilenames, ImageGroups
|
2013-03-18 22:04:09 +00:00
|
|
|
from openlp.plugins.images.lib.mediaitem import ImageMediaItem
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
2015-01-19 22:49:18 +00:00
|
|
|
Registry().register('application', MagicMock())
|
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())
|
2013-03-18 22:04:09 +00:00
|
|
|
mocked_plugin = MagicMock()
|
2013-10-02 21:07:20 +00:00
|
|
|
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)
|
2015-01-19 22:49:18 +00:00
|
|
|
self.media_item.settings_section = 'images'
|
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list')
|
|
|
|
@patch('openlp.plugins.images.lib.mediaitem.Settings')
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_validate_and_load(self, mocked_settings, mocked_load_list):
|
2015-01-19 22:49:18 +00:00
|
|
|
"""
|
2015-01-20 07:14:58 +00:00
|
|
|
Test that the validate_and_load_test() method when called without a group
|
2015-01-19 22:49:18 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: A list of files
|
2017-09-23 13:06:42 +00:00
|
|
|
file_list = [Path('path1', 'image1.jpg'), Path('path2', 'image2.jpg')]
|
2015-01-19 22:49:18 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
# WHEN: Calling validate_and_load with the list of files
|
|
|
|
self.media_item.validate_and_load(file_list)
|
2015-01-19 22:49:18 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
# THEN: load_list should have been called with the file list and None,
|
|
|
|
# the directory should have been saved to the settings
|
|
|
|
mocked_load_list.assert_called_once_with(file_list, None)
|
2017-09-23 13:06:42 +00:00
|
|
|
mocked_settings().setValue.assert_called_once_with(ANY, Path('path1'))
|
2015-01-19 22:49:18 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list')
|
|
|
|
@patch('openlp.plugins.images.lib.mediaitem.Settings')
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_validate_and_load_group(self, mocked_settings, mocked_load_list):
|
2015-01-19 22:49:18 +00:00
|
|
|
"""
|
2015-01-20 07:14:58 +00:00
|
|
|
Test that the validate_and_load_test() method when called with a group
|
2015-01-19 22:49:18 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: A list of files
|
2017-09-23 13:06:42 +00:00
|
|
|
file_list = [Path('path1', 'image1.jpg'), Path('path2', 'image2.jpg')]
|
2015-01-19 22:49:18 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
# WHEN: Calling validate_and_load with the list of files and a group
|
|
|
|
self.media_item.validate_and_load(file_list, 'group')
|
2015-01-19 22:49:18 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
# THEN: load_list should have been called with the file list and the group name,
|
|
|
|
# the directory should have been saved to the settings
|
|
|
|
mocked_load_list.assert_called_once_with(file_list, 'group')
|
2017-09-23 13:06:42 +00:00
|
|
|
mocked_settings().setValue.assert_called_once_with(ANY, Path('path1'))
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_save_new_images_list_empty_list(self, mocked_load_full_list):
|
2013-03-18 22:04:09 +00:00
|
|
|
"""
|
|
|
|
Test that the save_new_images_list() method handles empty lists gracefully
|
|
|
|
"""
|
|
|
|
# GIVEN: An empty image_list
|
|
|
|
image_list = []
|
2015-05-17 21:13:05 +00:00
|
|
|
self.media_item.manager = MagicMock()
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
# WHEN: We run save_new_images_list with the empty list
|
|
|
|
self.media_item.save_new_images_list(image_list)
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
# THEN: The save_object() method should not have been called
|
|
|
|
self.assertEquals(self.media_item.manager.save_object.call_count, 0,
|
|
|
|
'The save_object() method should not have been called')
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_save_new_images_list_single_image_with_reload(self, mocked_load_full_list):
|
2013-03-18 22:04:09 +00:00
|
|
|
"""
|
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
|
2013-03-18 22:04:09 +00:00
|
|
|
"""
|
2013-09-19 21:02:28 +00:00
|
|
|
# GIVEN: A list with 1 image and a mocked out manager
|
2017-09-23 13:06:42 +00:00
|
|
|
image_list = [Path('test_image.jpg')]
|
|
|
|
ImageFilenames.file_path = None
|
2015-05-17 21:13:05 +00:00
|
|
|
self.media_item.manager = MagicMock()
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
# WHEN: We run save_new_images_list with reload_list=True
|
|
|
|
self.media_item.save_new_images_list(image_list, reload_list=True)
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
# THEN: load_full_list() should have been called
|
|
|
|
self.assertEquals(mocked_load_full_list.call_count, 1, 'load_full_list() should have been called')
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
# CLEANUP: Remove added attribute from ImageFilenames
|
2017-09-23 13:06:42 +00:00
|
|
|
delattr(ImageFilenames, 'file_path')
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_save_new_images_list_single_image_without_reload(self, mocked_load_full_list):
|
2013-03-18 22:04:09 +00:00
|
|
|
"""
|
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
|
2013-03-18 22:04:09 +00:00
|
|
|
"""
|
2013-09-19 21:02:28 +00:00
|
|
|
# GIVEN: A list with 1 image and a mocked out manager
|
2017-09-23 13:06:42 +00:00
|
|
|
image_list = [Path('test_image.jpg')]
|
2015-05-17 21:13:05 +00:00
|
|
|
self.media_item.manager = MagicMock()
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
# WHEN: We run save_new_images_list with reload_list=False
|
|
|
|
self.media_item.save_new_images_list(image_list, reload_list=False)
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
# THEN: load_full_list() should not have been called
|
|
|
|
self.assertEquals(mocked_load_full_list.call_count, 0, 'load_full_list() should not have been called')
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_save_new_images_list_multiple_images(self, mocked_load_full_list):
|
2013-03-18 22:04:09 +00:00
|
|
|
"""
|
|
|
|
Test that the save_new_images_list() saves all images in the list
|
|
|
|
"""
|
|
|
|
# GIVEN: A list with 3 images
|
2017-09-23 13:06:42 +00:00
|
|
|
image_list = [Path('test_image_1.jpg'), Path('test_image_2.jpg'), Path('test_image_3.jpg')]
|
2015-05-17 21:13:05 +00:00
|
|
|
self.media_item.manager = MagicMock()
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
# WHEN: We run save_new_images_list with the list of 3 images
|
|
|
|
self.media_item.save_new_images_list(image_list, reload_list=False)
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
# THEN: load_full_list() should not have been called
|
|
|
|
self.assertEquals(self.media_item.manager.save_object.call_count, 3,
|
|
|
|
'load_full_list() should have been called three times')
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_save_new_images_list_other_objects_in_list(self, mocked_load_full_list):
|
2013-03-18 22:04:09 +00:00
|
|
|
"""
|
|
|
|
Test that the save_new_images_list() ignores everything in the provided list except strings
|
|
|
|
"""
|
|
|
|
# GIVEN: A list with images and objects
|
2017-09-23 13:06:42 +00:00
|
|
|
image_list = [Path('test_image_1.jpg'), None, True, ImageFilenames(), Path('test_image_2.jpg')]
|
2015-05-17 21:13:05 +00:00
|
|
|
self.media_item.manager = MagicMock()
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
# WHEN: We run save_new_images_list with the list of images and objects
|
|
|
|
self.media_item.save_new_images_list(image_list, reload_list=False)
|
2013-03-18 22:04:09 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
# THEN: load_full_list() should not have been called
|
|
|
|
self.assertEquals(self.media_item.manager.save_object.call_count, 2,
|
|
|
|
'load_full_list() should have been called only once')
|
2013-03-29 12:00:06 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_on_reset_click(self):
|
2013-03-29 12:00:06 +00:00
|
|
|
"""
|
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()
|
2017-11-15 18:33:21 +00:00
|
|
|
self.media_item.reset_action_context = 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)
|
2017-11-15 18:33:21 +00:00
|
|
|
self.media_item.reset_action_context.setVisible.assert_called_with(False)
|
2013-03-29 12:00:06 +00:00
|
|
|
self.media_item.live_controller.display.reset_image.assert_called_with()
|
2013-03-29 14:15:16 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
@patch('openlp.plugins.images.lib.mediaitem.delete_file')
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_recursively_delete_group(self, mocked_delete_file):
|
2013-03-29 14:15:16 +00:00
|
|
|
"""
|
|
|
|
Test that recursively_delete_group() works
|
|
|
|
"""
|
|
|
|
# GIVEN: An ImageGroups object and mocked functions
|
2015-05-17 21:13:05 +00:00
|
|
|
ImageFilenames.group_id = 1
|
|
|
|
ImageGroups.parent_id = 1
|
|
|
|
self.media_item.manager = MagicMock()
|
|
|
|
self.media_item.manager.get_all_objects.side_effect = self._recursively_delete_group_side_effect
|
2017-09-23 13:06:42 +00:00
|
|
|
self.media_item.service_path = Path()
|
2015-05-17 21:13:05 +00:00
|
|
|
test_group = ImageGroups()
|
|
|
|
test_group.id = 1
|
|
|
|
|
|
|
|
# WHEN: recursively_delete_group() is called
|
|
|
|
self.media_item.recursively_delete_group(test_group)
|
|
|
|
|
|
|
|
# THEN: delete_file() should have been called 12 times and manager.delete_object() 7 times.
|
|
|
|
self.assertEquals(mocked_delete_file.call_count, 12, 'delete_file() should have been called 12 times')
|
|
|
|
self.assertEquals(self.media_item.manager.delete_object.call_count, 7,
|
|
|
|
'manager.delete_object() should be called exactly 7 times')
|
|
|
|
|
|
|
|
# CLEANUP: Remove added attribute from Image Filenames and ImageGroups
|
|
|
|
delattr(ImageFilenames, 'group_id')
|
|
|
|
delattr(ImageGroups, 'parent_id')
|
2013-09-19 21:02:28 +00:00
|
|
|
|
|
|
|
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
|
2017-09-23 13:06:42 +00:00
|
|
|
returned_object1.file_path = Path('/', 'tmp', 'test_file_1.jpg')
|
2013-09-19 21:02:28 +00:00
|
|
|
returned_object2 = ImageFilenames()
|
|
|
|
returned_object2.id = 2
|
2017-09-23 13:06:42 +00:00
|
|
|
returned_object2.file_path = Path('/', 'tmp', 'test_file_2.jpg')
|
2013-09-19 21:02:28 +00:00
|
|
|
returned_object3 = ImageFilenames()
|
|
|
|
returned_object3.id = 3
|
2017-09-23 13:06:42 +00:00
|
|
|
returned_object3.file_path = Path('/', 'tmp', 'test_file_3.jpg')
|
2013-09-19 21:02:28 +00:00
|
|
|
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 []
|
2015-04-22 20:57:33 +00:00
|
|
|
|
2015-05-17 21:13:05 +00:00
|
|
|
@patch('openlp.plugins.images.lib.mediaitem.delete_file')
|
|
|
|
@patch('openlp.plugins.images.lib.mediaitem.check_item_selected')
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_on_delete_click(self, mocked_check_item_selected, mocked_delete_file):
|
2015-04-22 20:57:33 +00:00
|
|
|
"""
|
|
|
|
Test that on_delete_click() works
|
|
|
|
"""
|
|
|
|
# GIVEN: An ImageGroups object and mocked functions
|
2015-05-17 21:13:05 +00:00
|
|
|
mocked_check_item_selected.return_value = True
|
|
|
|
test_image = ImageFilenames()
|
|
|
|
test_image.id = 1
|
|
|
|
test_image.group_id = 1
|
2017-09-23 13:06:42 +00:00
|
|
|
test_image.file_path = Path('imagefile.png')
|
2015-05-17 21:13:05 +00:00
|
|
|
self.media_item.manager = MagicMock()
|
2017-09-23 13:06:42 +00:00
|
|
|
self.media_item.service_path = Path()
|
2015-05-17 21:13:05 +00:00
|
|
|
self.media_item.list_view = MagicMock()
|
|
|
|
mocked_row_item = MagicMock()
|
|
|
|
mocked_row_item.data.return_value = test_image
|
|
|
|
mocked_row_item.text.return_value = ''
|
|
|
|
self.media_item.list_view.selectedItems.return_value = [mocked_row_item]
|
|
|
|
|
|
|
|
# WHEN: Calling on_delete_click
|
|
|
|
self.media_item.on_delete_click()
|
|
|
|
|
|
|
|
# THEN: delete_file should have been called twice
|
|
|
|
self.assertEquals(mocked_delete_file.call_count, 2, 'delete_file() should have been called twice')
|
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_create_item_from_id(self):
|
2015-05-17 21:13:05 +00:00
|
|
|
"""
|
|
|
|
Test that the create_item_from_id() method returns a valid QTreeWidgetItem with a pre-created ImageFilenames
|
|
|
|
"""
|
|
|
|
# GIVEN: An ImageFilenames that already exists in the database
|
|
|
|
image_file = ImageFilenames()
|
|
|
|
image_file.id = 1
|
2017-09-23 13:06:42 +00:00
|
|
|
image_file.file_path = Path('/', 'tmp', 'test_file_1.jpg')
|
2015-05-17 21:13:05 +00:00
|
|
|
self.media_item.manager = MagicMock()
|
|
|
|
self.media_item.manager.get_object_filtered.return_value = image_file
|
2017-09-23 13:06:42 +00:00
|
|
|
ImageFilenames.file_path = None
|
2015-05-17 21:13:05 +00:00
|
|
|
|
|
|
|
# WHEN: create_item_from_id() is called
|
2017-09-23 13:06:42 +00:00
|
|
|
item = self.media_item.create_item_from_id('1')
|
2015-05-17 21:13:05 +00:00
|
|
|
|
|
|
|
# THEN: A QTreeWidgetItem should be created with the above model object as it's data
|
2015-11-07 00:49:40 +00:00
|
|
|
self.assertIsInstance(item, QtWidgets.QTreeWidgetItem)
|
2015-05-17 21:13:05 +00:00
|
|
|
self.assertEqual('test_file_1.jpg', item.text(0))
|
|
|
|
item_data = item.data(0, QtCore.Qt.UserRole)
|
|
|
|
self.assertIsInstance(item_data, ImageFilenames)
|
|
|
|
self.assertEqual(1, item_data.id)
|
2017-09-23 13:06:42 +00:00
|
|
|
self.assertEqual(Path('/', 'tmp', 'test_file_1.jpg'), item_data.file_path)
|