2014-07-02 19:18:18 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
2019-04-13 13:00:22 +00:00
|
|
|
##########################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
|
|
# Copyright (c) 2008-2019 OpenLP Developers #
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
|
|
# 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, either version 3 of the License, or #
|
|
|
|
# (at your option) any later version. #
|
|
|
|
# #
|
|
|
|
# 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, see <https://www.gnu.org/licenses/>. #
|
|
|
|
##########################################################################
|
2014-07-02 19:18:18 +00:00
|
|
|
"""
|
2014-06-23 13:51:56 +00:00
|
|
|
Package to test the openlp.core.ui.thememanager package.
|
2014-07-02 19:18:18 +00:00
|
|
|
"""
|
|
|
|
import os
|
2014-10-31 12:01:07 +00:00
|
|
|
import shutil
|
|
|
|
from tempfile import mkdtemp
|
2017-05-08 19:04:14 +00:00
|
|
|
from unittest import TestCase
|
|
|
|
from unittest.mock import ANY, MagicMock, patch
|
2014-07-02 19:18:18 +00:00
|
|
|
|
2015-11-07 00:49:40 +00:00
|
|
|
from PyQt5 import QtWidgets
|
2015-02-19 18:57:05 +00:00
|
|
|
|
2017-09-26 16:39:13 +00:00
|
|
|
from openlp.core.common.path import Path
|
2017-12-28 08:22:55 +00:00
|
|
|
from openlp.core.common.registry import Registry
|
2018-08-25 14:36:46 +00:00
|
|
|
from openlp.core.ui.thememanager import ThemeManager
|
2017-12-24 08:42:46 +00:00
|
|
|
from tests.utils.constants import RESOURCE_PATH
|
2014-07-02 19:18:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestThemeManager(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
2014-07-07 17:22:58 +00:00
|
|
|
Set up the tests
|
2014-07-02 19:18:18 +00:00
|
|
|
"""
|
|
|
|
Registry.create()
|
2015-04-02 09:04:56 +00:00
|
|
|
self.temp_folder = mkdtemp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""
|
|
|
|
Clean up
|
|
|
|
"""
|
|
|
|
shutil.rmtree(self.temp_folder)
|
2014-07-02 19:18:18 +00:00
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
@patch('openlp.core.ui.thememanager.zipfile.ZipFile.__init__')
|
|
|
|
@patch('openlp.core.ui.thememanager.zipfile.ZipFile.write')
|
|
|
|
def test_export_theme(self, mocked_zipfile_write, mocked_zipfile_init):
|
2014-06-23 13:51:56 +00:00
|
|
|
"""
|
|
|
|
Test exporting a theme .
|
|
|
|
"""
|
|
|
|
# GIVEN: A new ThemeManager instance.
|
|
|
|
theme_manager = ThemeManager()
|
2017-12-24 08:42:46 +00:00
|
|
|
theme_manager.theme_path = RESOURCE_PATH / 'themes'
|
2017-10-23 22:09:57 +00:00
|
|
|
mocked_zipfile_init.return_value = None
|
2014-06-23 13:51:56 +00:00
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
# WHEN: The theme is exported
|
|
|
|
theme_manager._export_theme(Path('some', 'path', 'Default.otz'), 'Default')
|
2014-06-23 13:51:56 +00:00
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
# THEN: The zipfile should be created at the given path
|
2019-03-10 21:01:39 +00:00
|
|
|
mocked_zipfile_init.assert_called_with(Path('some', 'path', 'Default.otz'), 'w')
|
|
|
|
mocked_zipfile_write.assert_called_with(RESOURCE_PATH / 'themes' / 'Default' / 'Default.xml',
|
|
|
|
Path('Default', 'Default.xml'))
|
2014-08-04 17:12:03 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_initial_theme_manager(self):
|
2014-07-02 19:18:18 +00:00
|
|
|
"""
|
2014-07-07 17:22:58 +00:00
|
|
|
Test the instantiation of theme manager.
|
2014-07-02 19:18:18 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: A new service manager instance.
|
|
|
|
ThemeManager(None)
|
|
|
|
|
|
|
|
# WHEN: the default theme manager is built.
|
|
|
|
# THEN: The the controller should be registered in the registry.
|
2017-12-20 21:18:44 +00:00
|
|
|
assert Registry().get('theme_manager') is not None, 'The base theme manager should be registered'
|
2014-07-02 19:18:18 +00:00
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
@patch('openlp.core.ui.thememanager.copyfile')
|
|
|
|
@patch('openlp.core.ui.thememanager.create_paths')
|
|
|
|
def test_write_theme_same_image(self, mocked_create_paths, mocked_copyfile):
|
2014-07-02 19:18:18 +00:00
|
|
|
"""
|
2014-07-07 17:22:58 +00:00
|
|
|
Test that we don't try to overwrite a theme background image with itself
|
2014-07-02 19:18:18 +00:00
|
|
|
"""
|
2017-09-26 16:39:13 +00:00
|
|
|
# GIVEN: A new theme manager instance, with mocked builtins.open, copyfile,
|
2017-10-07 07:05:07 +00:00
|
|
|
# theme, create_paths and thememanager-attributes.
|
2017-10-23 22:09:57 +00:00
|
|
|
theme_manager = ThemeManager(None)
|
|
|
|
theme_manager.old_background_image = None
|
|
|
|
theme_manager.generate_and_save_image = MagicMock()
|
|
|
|
theme_manager.theme_path = MagicMock()
|
|
|
|
mocked_theme = MagicMock()
|
|
|
|
mocked_theme.theme_name = 'themename'
|
|
|
|
mocked_theme.extract_formatted_xml = MagicMock()
|
|
|
|
mocked_theme.extract_formatted_xml.return_value = 'fake_theme_xml'.encode()
|
|
|
|
|
|
|
|
# WHEN: Calling _write_theme with path to the same image, but the path written slightly different
|
2017-12-24 08:42:46 +00:00
|
|
|
file_path_1 = RESOURCE_PATH / 'church.jpg'
|
|
|
|
theme_manager._write_theme(mocked_theme, file_path_1, file_path_1)
|
2017-10-23 22:09:57 +00:00
|
|
|
|
|
|
|
# THEN: The mocked_copyfile should not have been called
|
|
|
|
assert mocked_copyfile.called is False, 'copyfile should not be called'
|
|
|
|
|
|
|
|
@patch('openlp.core.ui.thememanager.copyfile')
|
|
|
|
@patch('openlp.core.ui.thememanager.create_paths')
|
|
|
|
def test_write_theme_diff_images(self, mocked_create_paths, mocked_copyfile):
|
2014-07-07 17:22:58 +00:00
|
|
|
"""
|
|
|
|
Test that we do overwrite a theme background image when a new is submitted
|
|
|
|
"""
|
2017-09-26 16:39:13 +00:00
|
|
|
# GIVEN: A new theme manager instance, with mocked builtins.open, copyfile,
|
2017-10-07 07:05:07 +00:00
|
|
|
# theme, create_paths and thememanager-attributes.
|
2017-10-23 22:09:57 +00:00
|
|
|
theme_manager = ThemeManager(None)
|
|
|
|
theme_manager.old_background_image = None
|
|
|
|
theme_manager.generate_and_save_image = MagicMock()
|
|
|
|
theme_manager.theme_path = MagicMock()
|
|
|
|
mocked_theme = MagicMock()
|
|
|
|
mocked_theme.theme_name = 'themename'
|
|
|
|
mocked_theme.filename = "filename"
|
|
|
|
|
|
|
|
# WHEN: Calling _write_theme with path to different images
|
2017-12-24 08:42:46 +00:00
|
|
|
file_path_1 = RESOURCE_PATH / 'church.jpg'
|
|
|
|
file_path_2 = RESOURCE_PATH / 'church2.jpg'
|
|
|
|
theme_manager._write_theme(mocked_theme, file_path_1, file_path_2)
|
2017-10-23 22:09:57 +00:00
|
|
|
|
|
|
|
# THEN: The mocked_copyfile should not have been called
|
|
|
|
assert mocked_copyfile.called is True, 'copyfile should be called'
|
2014-10-31 12:01:07 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_write_theme_special_char_name(self):
|
2015-04-02 09:04:56 +00:00
|
|
|
"""
|
|
|
|
Test that we can save themes with special characters in the name
|
|
|
|
"""
|
|
|
|
# GIVEN: A new theme manager instance, with mocked theme and thememanager-attributes.
|
|
|
|
theme_manager = ThemeManager(None)
|
|
|
|
theme_manager.old_background_image = None
|
|
|
|
theme_manager.generate_and_save_image = MagicMock()
|
2017-09-26 16:39:13 +00:00
|
|
|
theme_manager.theme_path = Path(self.temp_folder)
|
2015-04-02 09:04:56 +00:00
|
|
|
mocked_theme = MagicMock()
|
|
|
|
mocked_theme.theme_name = 'theme 愛 name'
|
2017-05-24 19:31:48 +00:00
|
|
|
mocked_theme.export_theme.return_value = "{}"
|
2015-04-02 09:04:56 +00:00
|
|
|
|
|
|
|
# WHEN: Calling _write_theme with a theme with a name with special characters in it
|
|
|
|
theme_manager._write_theme(mocked_theme, None, None)
|
|
|
|
|
|
|
|
# THEN: It should have been created
|
2017-12-20 21:18:44 +00:00
|
|
|
assert os.path.exists(os.path.join(self.temp_folder, 'theme 愛 name', 'theme 愛 name.json')) is True, \
|
|
|
|
'Theme with special characters should have been created!'
|
2015-04-02 09:04:56 +00:00
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
@patch('openlp.core.ui.thememanager.QtWidgets.QMessageBox.question', return_value=QtWidgets.QMessageBox.Yes)
|
|
|
|
@patch('openlp.core.ui.thememanager.translate')
|
|
|
|
def test_over_write_message_box_yes(self, mocked_translate, mocked_qmessagebox_question):
|
2015-02-19 18:57:05 +00:00
|
|
|
"""
|
|
|
|
Test that theme_manager.over_write_message_box returns True when the user clicks yes.
|
|
|
|
"""
|
|
|
|
# GIVEN: A patched QMessageBox.question and an instance of ThemeManager
|
2017-10-23 22:09:57 +00:00
|
|
|
mocked_translate.side_effect = lambda context, text: text
|
|
|
|
theme_manager = ThemeManager(None)
|
2015-02-19 18:57:05 +00:00
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
# WHEN: Calling over_write_message_box with 'Theme Name'
|
|
|
|
result = theme_manager.over_write_message_box('Theme Name')
|
2015-02-19 18:57:05 +00:00
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
# THEN: over_write_message_box should return True and the message box should contain the theme name
|
|
|
|
assert result is True
|
|
|
|
mocked_qmessagebox_question.assert_called_once_with(
|
|
|
|
theme_manager, 'Theme Already Exists', 'Theme Theme Name already exists. Do you want to replace it?',
|
|
|
|
defaultButton=ANY)
|
2015-02-19 18:57:05 +00:00
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
@patch('openlp.core.ui.thememanager.QtWidgets.QMessageBox.question', return_value=QtWidgets.QMessageBox.No)
|
|
|
|
@patch('openlp.core.ui.thememanager.translate')
|
|
|
|
def test_over_write_message_box_no(self, mocked_translate, mocked_qmessagebox_question):
|
2015-02-19 18:57:05 +00:00
|
|
|
"""
|
|
|
|
Test that theme_manager.over_write_message_box returns False when the user clicks no.
|
|
|
|
"""
|
|
|
|
# GIVEN: A patched QMessageBox.question and an instance of ThemeManager
|
2017-10-23 22:09:57 +00:00
|
|
|
mocked_translate.side_effect = lambda context, text: text
|
|
|
|
theme_manager = ThemeManager(None)
|
|
|
|
|
|
|
|
# WHEN: Calling over_write_message_box with 'Theme Name'
|
|
|
|
result = theme_manager.over_write_message_box('Theme Name')
|
|
|
|
|
|
|
|
# THEN: over_write_message_box should return False and the message box should contain the theme name
|
|
|
|
assert result is False
|
|
|
|
mocked_qmessagebox_question.assert_called_once_with(
|
|
|
|
theme_manager, 'Theme Already Exists', 'Theme Theme Name already exists. Do you want to replace it?',
|
|
|
|
defaultButton=ANY)
|
2015-02-19 18:57:05 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_unzip_theme(self):
|
2014-10-31 12:01:07 +00:00
|
|
|
"""
|
|
|
|
Test that unzipping of themes works
|
|
|
|
"""
|
|
|
|
# GIVEN: A theme file, a output folder and some mocked out internal functions
|
|
|
|
with patch('openlp.core.ui.thememanager.critical_error_message_box') \
|
|
|
|
as mocked_critical_error_message_box:
|
|
|
|
theme_manager = ThemeManager(None)
|
|
|
|
theme_manager._create_theme_from_xml = MagicMock()
|
|
|
|
theme_manager.generate_and_save_image = MagicMock()
|
2017-09-26 16:39:13 +00:00
|
|
|
theme_manager.theme_path = None
|
2017-11-18 11:23:15 +00:00
|
|
|
folder_path = Path(mkdtemp())
|
2017-12-24 08:42:46 +00:00
|
|
|
theme_file_path = RESOURCE_PATH / 'themes' / 'Moss_on_tree.otz'
|
2014-10-31 12:01:07 +00:00
|
|
|
|
|
|
|
# WHEN: We try to unzip it
|
2017-12-24 08:42:46 +00:00
|
|
|
theme_manager.unzip_theme(theme_file_path, folder_path)
|
2014-10-31 12:01:07 +00:00
|
|
|
|
|
|
|
# THEN: Files should be unpacked
|
2017-12-20 21:18:44 +00:00
|
|
|
assert (folder_path / 'Moss on tree' / 'Moss on tree.xml').exists() is True
|
|
|
|
assert mocked_critical_error_message_box.call_count == 0, 'No errors should have happened'
|
2017-11-18 11:23:15 +00:00
|
|
|
folder_path.rmtree()
|
2015-02-13 23:10:16 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_unzip_theme_invalid_version(self):
|
2015-02-13 23:10:16 +00:00
|
|
|
"""
|
|
|
|
Test that themes with invalid (< 2.0) or with no version attributes are rejected
|
|
|
|
"""
|
|
|
|
# GIVEN: An instance of ThemeManager whilst mocking a theme that returns a theme with no version attribute
|
|
|
|
with patch('openlp.core.ui.thememanager.zipfile.ZipFile') as mocked_zip_file,\
|
|
|
|
patch('openlp.core.ui.thememanager.ElementTree.getroot') as mocked_getroot,\
|
|
|
|
patch('openlp.core.ui.thememanager.XML'),\
|
|
|
|
patch('openlp.core.ui.thememanager.critical_error_message_box') as mocked_critical_error_message_box:
|
|
|
|
|
|
|
|
mocked_zip_file.return_value = MagicMock(**{'namelist.return_value': [os.path.join('theme', 'theme.xml')]})
|
|
|
|
mocked_getroot.return_value = MagicMock(**{'get.return_value': None})
|
|
|
|
theme_manager = ThemeManager(None)
|
|
|
|
|
|
|
|
# WHEN: unzip_theme is called
|
|
|
|
theme_manager.unzip_theme('theme.file', 'folder')
|
|
|
|
|
|
|
|
# THEN: The critical_error_message_box should have been called
|
2017-12-20 21:18:44 +00:00
|
|
|
assert mocked_critical_error_message_box.call_count == 1, 'Should have been called once'
|