From e3f147a226852be2a29d227d8fdd6b0016cf470e Mon Sep 17 00:00:00 2001 From: Phill Ridout Date: Mon, 19 Jan 2015 22:49:18 +0000 Subject: [PATCH] Fixes 1410456 and adds a couple tests Fixes: https://launchpad.net/bugs/1410456 --- openlp/plugins/images/lib/mediaitem.py | 4 +- tests/functional/__init__.py | 4 +- .../openlp_plugins/images/test_lib.py | 40 ++++++++++++++++++- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/openlp/plugins/images/lib/mediaitem.py b/openlp/plugins/images/lib/mediaitem.py index 9b3dd4d35..a1b3d7e71 100644 --- a/openlp/plugins/images/lib/mediaitem.py +++ b/openlp/plugins/images/lib/mediaitem.py @@ -371,7 +371,7 @@ class ImageMediaItem(MediaManagerItem): """ self.application.set_normal_cursor() self.load_list(files, target_group) - last_dir = os.path.split(str(files[0]))[0] + last_dir = os.path.split(files[0])[0] Settings().setValue(self.settings_section + '/last directory', last_dir) def load_list(self, images, target_group=None, initial_load=False): @@ -535,7 +535,7 @@ class ImageMediaItem(MediaManagerItem): if not items: return False # Determine service item title - if isinstance(items[0].data(0, QtCore.Qt.UserRole), ImageGroups): + if isinstance(items[0].data(0, QtCore.Qt.UserRole), ImageGroups) or len(items) == 1: service_item.title = items[0].text(0) else: service_item.title = str(self.plugin.name_strings['plural']) diff --git a/tests/functional/__init__.py b/tests/functional/__init__.py index 1ec85ff80..5948f480f 100644 --- a/tests/functional/__init__.py +++ b/tests/functional/__init__.py @@ -35,9 +35,9 @@ import sys from PyQt4 import QtGui if sys.version_info[1] >= 3: - from unittest.mock import MagicMock, patch, mock_open, call + from unittest.mock import ANY, MagicMock, patch, mock_open, call else: - from mock import MagicMock, patch, mock_open, call + from mock import ANY, MagicMock, patch, mock_open, call # Only one QApplication can be created. Use QtGui.QApplication.instance() when you need to "create" a QApplication. application = QtGui.QApplication([]) diff --git a/tests/functional/openlp_plugins/images/test_lib.py b/tests/functional/openlp_plugins/images/test_lib.py index d9f9f7603..2c9c94276 100644 --- a/tests/functional/openlp_plugins/images/test_lib.py +++ b/tests/functional/openlp_plugins/images/test_lib.py @@ -27,7 +27,7 @@ from unittest import TestCase 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 +from tests.functional import ANY, MagicMock, patch class TestImageMediaItem(TestCase): @@ -38,6 +38,7 @@ class TestImageMediaItem(TestCase): def setUp(self): self.mocked_main_window = MagicMock() Registry.create() + Registry().register('application', MagicMock()) Registry().register('service_list', MagicMock()) Registry().register('main_window', self.mocked_main_window) Registry().register('live_controller', MagicMock()) @@ -45,6 +46,43 @@ class TestImageMediaItem(TestCase): 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) + self.media_item.settings_section = 'images' + + def validate_and_load_test(self): + """ + Test that the validate_and_load_test() method + """ + # GIVEN: A list of files + file_list = ['/path1/image1.jpg', '/path2/image2.jpg'] + + with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list') as mocked_load_list, \ + patch('openlp.plugins.images.lib.mediaitem.Settings') as mocked_settings: + + # WHEN: Calling validate_and_load with the list of files + self.media_item.validate_and_load(file_list) + + # THEN: load_list should have been called with the file list and None, + # the dectory should have been saved to the settings + mocked_load_list.assert_called_once_with(file_list, None) + mocked_settings().setValue.assert_called_once_with(ANY, '/path1') + + def validate_and_load_group_test(self): + """ + Test that the validate_and_load_test() method + """ + # GIVEN: A list of files + file_list = ['/path1/image1.jpg', '/path2/image2.jpg'] + + with patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list') as mocked_load_list, \ + patch('openlp.plugins.images.lib.mediaitem.Settings') as mocked_settings: + + # WHEN: Calling validate_and_load with the list of files and a group + self.media_item.validate_and_load(file_list, 'group') + + # THEN: load_list should have been called with the file list and the group name, + # the dectory should have been saved to the settings + mocked_load_list.assert_called_once_with(file_list, 'group') + mocked_settings().setValue.assert_called_once_with(ANY, '/path1') def save_new_images_list_empty_list_test(self): """