openlp/tests/functional/openlp_plugins/presentations/test_powerpointcontroller.py

237 lines
11 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 #
###############################################################################
"""
Functional tests to test the PowerPointController class and related methods.
"""
import os
2014-07-03 11:21:12 +00:00
import shutil
from unittest import TestCase
from tempfile import mkdtemp
2014-07-03 11:21:12 +00:00
from tests.functional import patch, MagicMock
from tests.helpers.testmixin import TestMixin
2014-07-11 11:35:56 +00:00
from tests.utils.constants import TEST_RESOURCES_PATH
2014-07-11 11:35:56 +00:00
from openlp.plugins.presentations.lib.powerpointcontroller import PowerpointController, PowerpointDocument,\
2014-07-11 13:35:44 +00:00
_get_text_from_shapes
2014-09-08 21:48:49 +00:00
from openlp.core.common import is_win
if is_win():
import pywintypes
2013-12-30 18:19:50 +00:00
2014-07-03 11:21:12 +00:00
class TestPowerpointController(TestCase, TestMixin):
"""
Test the PowerpointController Class
"""
def setUp(self):
2014-07-03 11:21:12 +00:00
"""
Set up the patches and mocks need for all tests.
"""
self.setup_application()
2014-07-03 11:21:12 +00:00
self.build_settings()
self.mock_plugin = MagicMock()
self.temp_folder = mkdtemp()
self.mock_plugin.settings_section = self.temp_folder
2013-10-25 01:15:44 +00:00
2014-07-03 11:21:12 +00:00
def tearDown(self):
"""
2014-07-03 11:21:12 +00:00
Stop the patches
"""
2014-07-03 11:21:12 +00:00
self.destroy_settings()
shutil.rmtree(self.temp_folder)
2013-12-30 08:35:05 +00:00
2014-07-03 11:21:12 +00:00
def constructor_test(self):
"""
Test the Constructor from the PowerpointController
"""
# GIVEN: No presentation controller
controller = None
# WHEN: The presentation controller object is created
controller = PowerpointController(plugin=self.mock_plugin)
# THEN: The name of the presentation controller should be correct
self.assertEqual('Powerpoint', controller.name,
'The name of the presentation controller should be correct')
2014-07-11 11:35:56 +00:00
class TestPowerpointDocument(TestCase, TestMixin):
2014-07-03 11:21:12 +00:00
"""
Test the PowerpointDocument Class
"""
def setUp(self):
"""
Set up the patches and mocks need for all tests.
"""
self.setup_application()
2014-07-11 11:35:56 +00:00
self.build_settings()
self.mock_plugin = MagicMock()
self.temp_folder = mkdtemp()
self.mock_plugin.settings_section = self.temp_folder
2014-07-03 11:21:12 +00:00
self.powerpoint_document_stop_presentation_patcher = patch(
'openlp.plugins.presentations.lib.powerpointcontroller.PowerpointDocument.stop_presentation')
self.presentation_document_get_temp_folder_patcher = patch(
'openlp.plugins.presentations.lib.powerpointcontroller.PresentationDocument.get_temp_folder')
self.presentation_document_setup_patcher = patch(
'openlp.plugins.presentations.lib.powerpointcontroller.PresentationDocument._setup')
self.mock_powerpoint_document_stop_presentation = self.powerpoint_document_stop_presentation_patcher.start()
self.mock_presentation_document_get_temp_folder = self.presentation_document_get_temp_folder_patcher.start()
self.mock_presentation_document_setup = self.presentation_document_setup_patcher.start()
self.mock_controller = MagicMock()
self.mock_presentation = MagicMock()
self.mock_presentation_document_get_temp_folder.return_value = 'temp folder'
self.file_name = os.path.join(TEST_RESOURCES_PATH, 'presentations', 'test.pptx')
2014-07-11 11:35:56 +00:00
self.real_controller = PowerpointController(self.mock_plugin)
2014-07-03 11:21:12 +00:00
def tearDown(self):
"""
Stop the patches
"""
self.powerpoint_document_stop_presentation_patcher.stop()
self.presentation_document_get_temp_folder_patcher.stop()
self.presentation_document_setup_patcher.stop()
2014-07-11 11:35:56 +00:00
self.destroy_settings()
shutil.rmtree(self.temp_folder)
2014-07-03 11:21:12 +00:00
def show_error_msg_test(self):
"""
Test the PowerpointDocument.show_error_msg() method gets called on com exception
"""
2014-09-08 21:48:49 +00:00
if is_win():
2014-07-03 11:21:12 +00:00
# GIVEN: A PowerpointDocument with mocked controller and presentation
with patch('openlp.plugins.presentations.lib.powerpointcontroller.critical_error_message_box') as \
mocked_critical_error_message_box:
instance = PowerpointDocument(self.mock_controller, self.mock_presentation)
instance.presentation = MagicMock()
instance.presentation.SlideShowWindow.View.GotoSlide = MagicMock(side_effect=pywintypes.com_error('1'))
# WHEN: Calling goto_slide which will throw an exception
instance.goto_slide(42)
# THEN: mocked_critical_error_message_box should have been called
mocked_critical_error_message_box.assert_called_with('Error', 'An error occurred in the Powerpoint '
'integration and the presentation will be stopped.'
' Restart the presentation if you wish to '
'present it.')
# add _test to the following if necessary
def verify_loading_document(self):
"""
Test loading a document in PowerPoint
2014-07-11 11:35:56 +00:00
"""
2014-09-08 21:48:49 +00:00
if is_win() and self.real_controller.check_available():
2014-07-11 11:35:56 +00:00
# GIVEN: A PowerpointDocument and a presentation
doc = PowerpointDocument(self.real_controller, self.file_name)
2013-12-30 08:35:05 +00:00
2014-07-11 11:35:56 +00:00
# WHEN: loading the filename
doc.load_presentation()
result = doc.is_loaded()
2013-12-30 08:35:05 +00:00
2014-07-11 11:35:56 +00:00
# THEN: result should be true
self.assertEqual(result, True, 'The result should be True')
else:
self.skipTest('Powerpoint not available, skipping test.')
2013-10-25 01:15:44 +00:00
def create_titles_and_notes_test(self):
"""
2013-10-25 01:15:44 +00:00
Test creating the titles from PowerPoint
"""
2014-09-08 21:48:49 +00:00
if is_win() and self.real_controller.check_available():
2014-07-11 11:35:56 +00:00
# GIVEN: mocked save_titles_and_notes, _get_text_from_shapes and two mocked slides
self.doc = PowerpointDocument(self.real_controller, self.file_name)
self.doc.save_titles_and_notes = MagicMock()
self.doc._PowerpointDocument__get_text_from_shapes = MagicMock()
slide = MagicMock()
slide.Shapes.Title.TextFrame.TextRange.Text = 'SlideText'
pres = MagicMock()
pres.Slides = [slide, slide]
self.doc.presentation = pres
# WHEN reading the titles and notes
self.doc.create_titles_and_notes()
# THEN the save should have been called exactly once with 2 titles and 2 notes
self.doc.save_titles_and_notes.assert_called_once_with(['SlideText\n', 'SlideText\n'], [' ', ' '])
else:
self.skipTest('Powerpoint not available, skipping test.')
2013-10-25 01:15:44 +00:00
def create_titles_and_notes_with_no_slides_test(self):
"""
Test creating the titles from PowerPoint when it returns no slides
"""
2014-09-08 21:48:49 +00:00
if is_win() and self.real_controller.check_available():
2014-07-11 11:35:56 +00:00
# GIVEN: mocked save_titles_and_notes, _get_text_from_shapes and two mocked slides
doc = PowerpointDocument(self.real_controller, self.file_name)
doc.save_titles_and_notes = MagicMock()
doc._PowerpointDocument__get_text_from_shapes = MagicMock()
pres = MagicMock()
pres.Slides = []
doc.presentation = pres
# WHEN reading the titles and notes
doc.create_titles_and_notes()
# THEN the save should have been called exactly once with empty titles and notes
doc.save_titles_and_notes.assert_called_once_with([], [])
else:
self.skipTest('Powerpoint not available, skipping test.')
2013-10-25 01:15:44 +00:00
def get_text_from_shapes_test(self):
"""
2014-07-11 13:35:44 +00:00
Test getting text from powerpoint shapes
2013-10-25 01:15:44 +00:00
"""
2013-12-30 08:35:05 +00:00
# GIVEN: mocked shapes
2013-10-25 01:15:44 +00:00
shape = MagicMock()
shape.PlaceholderFormat.Type = 2
shape.HasTextFrame = shape.TextFrame.HasText = True
shape.TextFrame.TextRange.Text = 'slideText'
2013-10-28 15:16:22 +00:00
shapes = [shape, shape]
2013-12-30 08:35:05 +00:00
# WHEN: getting the text
2013-10-25 01:15:44 +00:00
result = _get_text_from_shapes(shapes)
2013-12-30 08:35:05 +00:00
# THEN: it should return the text
2013-12-30 18:19:50 +00:00
self.assertEqual(result, 'slideText\nslideText\n', 'result should match \'slideText\nslideText\n\'')
2013-10-28 02:33:28 +00:00
2013-10-25 01:15:44 +00:00
def get_text_from_shapes_with_no_shapes_test(self):
"""
Test getting text from powerpoint shapes with no shapes
"""
2013-12-30 08:35:05 +00:00
# GIVEN: empty shapes array
2013-10-25 01:15:44 +00:00
shapes = []
2013-12-30 08:35:05 +00:00
# WHEN: getting the text
2013-10-25 01:15:44 +00:00
result = _get_text_from_shapes(shapes)
2013-12-30 08:35:05 +00:00
# THEN: it should not fail but return empty string
2013-10-28 02:33:28 +00:00
self.assertEqual(result, '', 'result should be empty')