2014-07-02 19:18:18 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# Copyright (c) 2008-2014 Raoul Snyman #
|
|
|
|
# Portions copyright (c) 2008-2014 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 #
|
|
|
|
###############################################################################
|
|
|
|
"""
|
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
|
|
|
"""
|
2014-06-23 13:51:56 +00:00
|
|
|
import zipfile
|
2014-07-02 19:18:18 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
from unittest import TestCase
|
2014-06-23 13:51:56 +00:00
|
|
|
from tests.interfaces import MagicMock
|
2014-07-02 19:18:18 +00:00
|
|
|
|
|
|
|
from openlp.core.ui import ThemeManager
|
2014-08-04 17:12:03 +00:00
|
|
|
from openlp.core.common import Registry
|
2014-07-02 19:18:18 +00:00
|
|
|
|
|
|
|
from tests.utils.constants import TEST_RESOURCES_PATH
|
|
|
|
from tests.interfaces import MagicMock, patch
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2014-06-23 13:51:56 +00:00
|
|
|
def export_theme_test(self):
|
|
|
|
"""
|
|
|
|
Test exporting a theme .
|
|
|
|
"""
|
|
|
|
# GIVEN: A new ThemeManager instance.
|
|
|
|
theme_manager = ThemeManager()
|
2014-08-04 17:12:03 +00:00
|
|
|
theme_manager.path = os.path.join(TEST_RESOURCES_PATH, 'themes')
|
2014-06-23 13:51:56 +00:00
|
|
|
zipfile.ZipFile.__init__ = MagicMock()
|
|
|
|
zipfile.ZipFile.__init__.return_value = None
|
|
|
|
zipfile.ZipFile.write = MagicMock()
|
|
|
|
|
|
|
|
# WHEN: The theme is exported
|
|
|
|
theme_manager._export_theme('/some/path', 'Default')
|
|
|
|
|
|
|
|
# THEN: The zipfile should be created at the given path
|
|
|
|
zipfile.ZipFile.__init__.assert_called_with('/some/path/Default.otz', 'w')
|
2014-08-04 17:12:03 +00:00
|
|
|
zipfile.ZipFile.write.assert_called_with(os.path.join(TEST_RESOURCES_PATH, 'themes', 'Default', 'Default.xml'),
|
2014-06-23 13:51:56 +00:00
|
|
|
'Default/Default.xml')
|
2014-08-04 17:12:03 +00:00
|
|
|
|
2014-07-02 19:18:18 +00:00
|
|
|
def initial_theme_manager_test(self):
|
|
|
|
"""
|
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.
|
2014-07-07 17:22:58 +00:00
|
|
|
self.assertIsNotNone(Registry().get('theme_manager'), 'The base theme manager should be registered')
|
2014-07-02 19:18:18 +00:00
|
|
|
|
2014-07-07 17:22:58 +00:00
|
|
|
def write_theme_same_image_test(self):
|
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
|
|
|
"""
|
|
|
|
# GIVEN: A new theme manager instance, with mocked builtins.open, shutil.copyfile,
|
|
|
|
# theme, check_directory_exists and thememanager-attributes.
|
|
|
|
with patch('builtins.open') as mocked_open, \
|
|
|
|
patch('openlp.core.ui.thememanager.shutil.copyfile') as mocked_copyfile, \
|
|
|
|
patch('openlp.core.ui.thememanager.check_directory_exists') as mocked_check_directory_exists:
|
|
|
|
mocked_open.return_value = MagicMock()
|
|
|
|
theme_manager = ThemeManager(None)
|
|
|
|
theme_manager.old_background_image = None
|
|
|
|
theme_manager.generate_and_save_image = MagicMock()
|
|
|
|
theme_manager.path = ''
|
|
|
|
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
|
|
|
|
file_name1 = os.path.join(TEST_RESOURCES_PATH, 'church.jpg')
|
|
|
|
# Do replacement from end of string to avoid problems with path start
|
|
|
|
file_name2 = file_name1[::-1].replace(os.sep, os.sep + os.sep, 2)[::-1]
|
|
|
|
theme_manager._write_theme(mocked_theme, file_name1, file_name2)
|
|
|
|
|
|
|
|
# THEN: The mocked_copyfile should not have been called
|
|
|
|
self.assertFalse(mocked_copyfile.called, 'shutil.copyfile should not be called')
|
2014-07-07 17:22:58 +00:00
|
|
|
|
|
|
|
def write_theme_diff_images_test(self):
|
|
|
|
"""
|
|
|
|
Test that we do overwrite a theme background image when a new is submitted
|
|
|
|
"""
|
|
|
|
# GIVEN: A new theme manager instance, with mocked builtins.open, shutil.copyfile,
|
|
|
|
# theme, check_directory_exists and thememanager-attributes.
|
|
|
|
with patch('builtins.open') as mocked_open, \
|
|
|
|
patch('openlp.core.ui.thememanager.shutil.copyfile') as mocked_copyfile, \
|
|
|
|
patch('openlp.core.ui.thememanager.check_directory_exists') as mocked_check_directory_exists:
|
|
|
|
mocked_open.return_value = MagicMock()
|
|
|
|
theme_manager = ThemeManager(None)
|
|
|
|
theme_manager.old_background_image = None
|
|
|
|
theme_manager.generate_and_save_image = MagicMock()
|
|
|
|
theme_manager.path = ''
|
|
|
|
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 different images
|
|
|
|
file_name1 = os.path.join(TEST_RESOURCES_PATH, 'church.jpg')
|
|
|
|
file_name2 = os.path.join(TEST_RESOURCES_PATH, 'church2.jpg')
|
|
|
|
theme_manager._write_theme(mocked_theme, file_name1, file_name2)
|
|
|
|
|
|
|
|
# THEN: The mocked_copyfile should not have been called
|
|
|
|
self.assertTrue(mocked_copyfile.called, 'shutil.copyfile should be called')
|