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

164 lines
8.4 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 #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2013 Raoul Snyman #
# Portions copyright (c) 2008-2013 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 Impress class and related methods.
"""
from unittest import TestCase
import os
2013-10-28 02:33:28 +00:00
from mock import MagicMock
from openlp.plugins.presentations.lib.impresscontroller import \
ImpressController, ImpressDocument, TextType
2013-10-28 02:33:28 +00:00
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..',
'..', '..', 'resources'))
class TestLibModule(TestCase):
def setUp(self):
mocked_plugin = MagicMock()
mocked_plugin.settings_section = 'presentations'
2013-10-28 15:16:22 +00:00
self.file_name = os.path.join(TEST_PATH, 'test.pptx')
self.ppc = ImpressController(mocked_plugin)
2013-10-28 15:16:22 +00:00
self.doc = ImpressDocument(self.ppc, self.file_name)
2013-10-23 03:12:45 +00:00
def create_titles_and_notes_test(self):
"""
2013-10-23 03:12:45 +00:00
Test ImpressDocument.create_titles_and_notes
"""
2013-10-28 02:33:28 +00:00
# GIVEN: mocked PresentationController.save_titles_and_notes with
# 0 pages and the LibreOffice Document
2013-10-23 03:12:45 +00:00
self.doc.save_titles_and_notes = MagicMock()
self.doc.document = MagicMock()
self.doc.document.getDrawPages.return_value = MagicMock()
self.doc.document.getDrawPages().getCount.return_value = 0
# WHEN reading the titles and notes
self.doc.create_titles_and_notes()
2013-10-28 02:33:28 +00:00
# THEN save_titles_and_notes should have been called with empty arrays
2013-10-28 15:16:22 +00:00
self.doc.save_titles_and_notes.assert_called_once_with([], [])
2013-10-23 03:12:45 +00:00
# GIVEN: reset mock and set it to 2 pages
self.doc.save_titles_and_notes.reset_mock()
self.doc.document.getDrawPages().getCount.return_value = 2
# WHEN: a new call to create_titles_and_notes
self.doc.create_titles_and_notes()
2013-10-28 02:33:28 +00:00
# THEN: save_titles_and_notes should have been called once with
# two arrays of two elements
self.doc.save_titles_and_notes.assert_called_once_with(
2013-10-28 15:16:22 +00:00
['\n', '\n'], [' ', ' '])
2013-10-23 03:12:45 +00:00
def get_text_from_page_out_of_bound_test(self):
"""
2013-10-23 03:12:45 +00:00
Test ImpressDocument.__get_text_from_page with out-of-bounds index
"""
2013-10-28 02:33:28 +00:00
# GIVEN: mocked LibreOffice Document with one slide,
# two notes and three texts
2013-10-28 15:16:22 +00:00
self.doc.document = self._mock_a_LibreOffice_document(1, 2, 3)
2013-10-28 02:33:28 +00:00
# WHEN: __get_text_from_page is called with an index of 0x00
2013-10-28 15:16:22 +00:00
result = self.doc._ImpressDocument__get_text_from_page(0, TextType.Notes)
2013-10-23 03:12:45 +00:00
# THEN: the result should be an empty string
2013-10-28 02:33:28 +00:00
self.assertEqual(result, '', 'Result should be an empty string')
2013-10-23 03:12:45 +00:00
# WHEN: regardless of the type of text, index 0x00 is out of bounds
2013-10-28 15:16:22 +00:00
result = self.doc._ImpressDocument__get_text_from_page(0, TextType.Title)
2013-10-23 03:12:45 +00:00
# THEN: result should be an empty string
2013-10-28 02:33:28 +00:00
self.assertEqual(result, '', 'Result should be an empty string')
2013-10-23 03:12:45 +00:00
# WHEN: when called with 2, it should also be out of bounds
2013-10-28 15:16:22 +00:00
result = self.doc._ImpressDocument__get_text_from_page(2, TextType.SlideText)
2013-10-28 02:33:28 +00:00
# THEN: result should be an empty string ... and, getByIndex should
# have never been called
self.assertEqual(result, '', 'Result should be an empty string')
self.assertEqual(self.doc.document.getDrawPages().getByIndex.call_count,
0, 'There should be no call to getByIndex')
2013-10-23 03:12:45 +00:00
def get_text_from_page_wrong_type_test(self):
"""
Test ImpressDocument.__get_text_from_page with wrong TextType
"""
2013-10-28 02:33:28 +00:00
# GIVEN: mocked LibreOffice Document with one slide, two notes and
# three texts
2013-10-28 15:16:22 +00:00
self.doc.document = self._mock_a_LibreOffice_document(1, 2, 3)
2013-10-23 03:12:45 +00:00
# WHEN: called with TextType 3
2013-10-28 15:16:22 +00:00
result = self.doc._ImpressDocument__get_text_from_page(1, 3)
2013-10-23 03:12:45 +00:00
# THEN: result should be an empty string
2013-10-28 02:33:28 +00:00
self.assertEqual(result, '', 'Result should be and empty string')
self.assertEqual(self.doc.document.getDrawPages().getByIndex.call_count,
0, 'There should be no call to getByIndex')
2013-10-23 03:12:45 +00:00
def get_text_from_page_valid_params_test(self):
"""
Test ImpressDocument.__get_text_from_page with valid parameters
"""
2013-10-28 02:33:28 +00:00
# GIVEN: mocked LibreOffice Document with one slide,
# two notes and three texts
2013-10-28 15:16:22 +00:00
self.doc.document = self._mock_a_LibreOffice_document(1, 2, 3)
2013-10-23 03:12:45 +00:00
# WHEN: __get_text_from_page is called to get the Notes
2013-10-28 02:33:28 +00:00
result = self.doc._ImpressDocument__get_text_from_page(1, TextType.Notes)
2013-10-23 03:12:45 +00:00
# THEN: result should be 'Note\nNote\n'
2013-10-28 02:33:28 +00:00
self.assertEqual(result, 'Note\nNote\n',
'Result should be \'Note\\n\' times the count of notes in the page')
2013-10-23 03:12:45 +00:00
# WHEN: get the Title
2013-10-28 02:33:28 +00:00
result = self.doc._ImpressDocument__get_text_from_page(1, TextType.Title)
2013-10-23 03:12:45 +00:00
# THEN: result should be 'Title\n'
2013-10-28 02:33:28 +00:00
self.assertEqual(result, 'Title\n',
'Result should be exactly \'Title\\n\'')
2013-10-23 03:12:45 +00:00
# WHEN: get all text
2013-10-28 02:33:28 +00:00
result = self.doc._ImpressDocument__get_text_from_page(1,
TextType.SlideText)
2013-10-23 03:12:45 +00:00
# THEN: result should be 'Title\nString\nString\n'
2013-10-28 02:33:28 +00:00
self.assertEqual(result, 'Title\nString\nString\n',
'Result should be exactly \'Title\\nString\\nString\\n\'')
2013-10-25 01:15:44 +00:00
2013-10-28 02:33:28 +00:00
def _mock_a_LibreOffice_document(self, page_count, note_count, text_count):
2013-10-25 01:15:44 +00:00
pages = MagicMock()
page = MagicMock()
pages.getByIndex.return_value = page
2013-10-28 02:33:28 +00:00
notes_page = MagicMock()
notes_page.getCount.return_value = note_count
2013-10-25 01:15:44 +00:00
shape = MagicMock()
shape.supportsService.return_value = True
shape.getString.return_value = 'Note'
2013-10-28 02:33:28 +00:00
notes_page.getByIndex.return_value = shape
page.getNotesPage.return_value = notes_page
page.getCount.return_value = text_count
2013-10-25 01:15:44 +00:00
page.getByIndex.side_effect = self._get_page_shape_side_effect
2013-10-28 02:33:28 +00:00
pages.getCount.return_value = page_count
2013-10-25 01:15:44 +00:00
document = MagicMock()
document.getDrawPages.return_value = pages
document.getByIndex.return_value = page
return document
2013-10-28 02:33:28 +00:00
def _get_page_shape_side_effect(*args):
page_shape = MagicMock()
page_shape.supportsService.return_value = True
2013-10-25 01:15:44 +00:00
if args[1] == 0:
2013-10-28 02:33:28 +00:00
page_shape.getShapeType.return_value = \
'com.sun.star.presentation.TitleTextShape'
page_shape.getString.return_value = 'Title'
2013-10-25 01:15:44 +00:00
else:
2013-10-28 02:33:28 +00:00
page_shape.getString.return_value = 'String'
return page_shape