2013-11-07 20:33:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2016-12-31 11:01:36 +00:00
|
|
|
# Copyright (c) 2008-2017 OpenLP Developers #
|
2013-11-07 20:33:55 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# 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 #
|
|
|
|
###############################################################################
|
|
|
|
"""
|
|
|
|
This module contains tests for the pptviewcontroller module of the Presentations plugin.
|
|
|
|
"""
|
2014-03-24 16:49:50 +00:00
|
|
|
import shutil
|
|
|
|
from tempfile import mkdtemp
|
2013-11-16 10:05:41 +00:00
|
|
|
from unittest import TestCase
|
2017-04-24 05:17:55 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
2014-03-24 16:49:50 +00:00
|
|
|
|
|
|
|
from openlp.plugins.presentations.lib.pptviewcontroller import PptviewDocument, PptviewController
|
2014-09-08 21:48:49 +00:00
|
|
|
from openlp.core.common import is_win
|
2017-09-17 19:43:15 +00:00
|
|
|
from openlp.core.common.path import Path
|
2014-09-08 21:48:49 +00:00
|
|
|
|
2017-04-24 05:17:55 +00:00
|
|
|
from tests.helpers.testmixin import TestMixin
|
|
|
|
from tests.utils.constants import TEST_RESOURCES_PATH
|
|
|
|
|
2014-09-08 21:48:49 +00:00
|
|
|
if is_win():
|
|
|
|
from ctypes import cdll
|
2013-11-16 10:16:06 +00:00
|
|
|
|
2013-11-07 20:33:55 +00:00
|
|
|
|
2014-03-24 16:49:50 +00:00
|
|
|
class TestPptviewController(TestCase, TestMixin):
|
|
|
|
"""
|
|
|
|
Test the PptviewController Class
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Set up the patches and mocks need for all tests.
|
|
|
|
"""
|
2014-10-22 20:43:05 +00:00
|
|
|
self.setup_application()
|
2014-03-24 16:49:50 +00:00
|
|
|
self.build_settings()
|
|
|
|
self.mock_plugin = MagicMock()
|
|
|
|
self.temp_folder = mkdtemp()
|
|
|
|
self.mock_plugin.settings_section = self.temp_folder
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""
|
|
|
|
Stop the patches
|
|
|
|
"""
|
|
|
|
self.destroy_settings()
|
|
|
|
shutil.rmtree(self.temp_folder)
|
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_constructor(self):
|
2014-03-24 16:49:50 +00:00
|
|
|
"""
|
|
|
|
Test the Constructor from the PptViewController
|
|
|
|
"""
|
|
|
|
# GIVEN: No presentation controller
|
|
|
|
controller = None
|
|
|
|
|
|
|
|
# WHEN: The presentation controller object is created
|
|
|
|
controller = PptviewController(plugin=self.mock_plugin)
|
|
|
|
|
|
|
|
# THEN: The name of the presentation controller should be correct
|
2014-04-02 19:35:09 +00:00
|
|
|
self.assertEqual('Powerpoint Viewer', controller.name,
|
|
|
|
'The name of the presentation controller should be correct')
|
2014-03-24 16:49:50 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_check_available(self):
|
2014-03-24 16:49:50 +00:00
|
|
|
"""
|
|
|
|
Test check_available / check_installed
|
|
|
|
"""
|
|
|
|
# GIVEN: A mocked dll loader and a controller
|
|
|
|
with patch('ctypes.cdll.LoadLibrary') as mocked_load_library:
|
|
|
|
mocked_process = MagicMock()
|
|
|
|
mocked_process.CheckInstalled.return_value = True
|
|
|
|
mocked_load_library.return_value = mocked_process
|
|
|
|
controller = PptviewController(plugin=self.mock_plugin)
|
|
|
|
|
|
|
|
# WHEN: check_available is called
|
|
|
|
available = controller.check_available()
|
|
|
|
|
|
|
|
# THEN: On windows it should return True, on other platforms False
|
2014-09-08 21:48:49 +00:00
|
|
|
if is_win():
|
2014-04-02 19:35:09 +00:00
|
|
|
self.assertTrue(available, 'check_available should return True on windows.')
|
2014-03-24 16:49:50 +00:00
|
|
|
else:
|
2014-04-02 19:35:09 +00:00
|
|
|
self.assertFalse(available, 'check_available should return False when not on windows.')
|
2014-03-24 16:49:50 +00:00
|
|
|
|
2014-03-04 20:18:14 +00:00
|
|
|
|
2013-11-07 20:33:55 +00:00
|
|
|
class TestPptviewDocument(TestCase):
|
|
|
|
"""
|
|
|
|
Test the PptviewDocument Class
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Set up the patches and mocks need for all tests.
|
|
|
|
"""
|
2014-07-15 09:45:23 +00:00
|
|
|
self.os_isdir_patcher = patch('openlp.plugins.presentations.lib.pptviewcontroller.os.path.isdir')
|
2013-11-07 20:33:55 +00:00
|
|
|
self.pptview_document_create_thumbnails_patcher = patch(
|
|
|
|
'openlp.plugins.presentations.lib.pptviewcontroller.PptviewDocument.create_thumbnails')
|
|
|
|
self.pptview_document_stop_presentation_patcher = patch(
|
|
|
|
'openlp.plugins.presentations.lib.pptviewcontroller.PptviewDocument.stop_presentation')
|
|
|
|
self.presentation_document_get_temp_folder_patcher = patch(
|
|
|
|
'openlp.plugins.presentations.lib.pptviewcontroller.PresentationDocument.get_temp_folder')
|
|
|
|
self.presentation_document_setup_patcher = patch(
|
|
|
|
'openlp.plugins.presentations.lib.pptviewcontroller.PresentationDocument._setup')
|
|
|
|
self.screen_list_patcher = patch('openlp.plugins.presentations.lib.pptviewcontroller.ScreenList')
|
2014-03-04 20:18:14 +00:00
|
|
|
self.rect_patcher = MagicMock()
|
2014-07-15 09:45:23 +00:00
|
|
|
self.mock_os_isdir = self.os_isdir_patcher.start()
|
2013-11-07 20:33:55 +00:00
|
|
|
self.mock_pptview_document_create_thumbnails = self.pptview_document_create_thumbnails_patcher.start()
|
|
|
|
self.mock_pptview_document_stop_presentation = self.pptview_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_rect = self.rect_patcher.start()
|
|
|
|
self.mock_screen_list = self.screen_list_patcher.start()
|
|
|
|
self.mock_controller = MagicMock()
|
|
|
|
self.mock_presentation = MagicMock()
|
2014-07-14 14:15:08 +00:00
|
|
|
self.temp_folder = mkdtemp()
|
|
|
|
self.mock_presentation_document_get_temp_folder.return_value = self.temp_folder
|
2013-11-07 20:33:55 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""
|
|
|
|
Stop the patches
|
|
|
|
"""
|
2014-07-15 09:45:23 +00:00
|
|
|
self.os_isdir_patcher.stop()
|
2013-11-07 20:33:55 +00:00
|
|
|
self.pptview_document_create_thumbnails_patcher.stop()
|
|
|
|
self.pptview_document_stop_presentation_patcher.stop()
|
|
|
|
self.presentation_document_get_temp_folder_patcher.stop()
|
|
|
|
self.presentation_document_setup_patcher.stop()
|
|
|
|
self.rect_patcher.stop()
|
|
|
|
self.screen_list_patcher.stop()
|
2014-07-14 14:15:08 +00:00
|
|
|
shutil.rmtree(self.temp_folder)
|
2013-11-07 20:33:55 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_load_presentation_succesfull(self):
|
2013-11-07 20:33:55 +00:00
|
|
|
"""
|
|
|
|
Test the PptviewDocument.load_presentation() method when the PPT is successfully opened
|
|
|
|
"""
|
|
|
|
# GIVEN: A reset mocked_os
|
2014-07-15 09:45:23 +00:00
|
|
|
self.mock_os_isdir.reset()
|
2013-11-07 20:33:55 +00:00
|
|
|
|
|
|
|
# WHEN: The temporary directory exists and OpenPPT returns successfully (not -1)
|
2014-07-15 09:45:23 +00:00
|
|
|
self.mock_os_isdir.return_value = True
|
2013-11-07 20:33:55 +00:00
|
|
|
self.mock_controller.process.OpenPPT.return_value = 0
|
|
|
|
instance = PptviewDocument(self.mock_controller, self.mock_presentation)
|
2014-03-08 21:23:47 +00:00
|
|
|
instance.file_path = 'test\path.ppt'
|
2013-11-07 20:33:55 +00:00
|
|
|
|
2014-09-08 21:48:49 +00:00
|
|
|
if is_win():
|
2014-03-04 20:18:14 +00:00
|
|
|
result = instance.load_presentation()
|
|
|
|
|
|
|
|
# THEN: PptviewDocument.load_presentation should return True
|
|
|
|
self.assertTrue(result)
|
2013-11-07 20:33:55 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_load_presentation_un_succesfull(self):
|
2013-11-07 20:33:55 +00:00
|
|
|
"""
|
|
|
|
Test the PptviewDocument.load_presentation() method when the temporary directory does not exist and the PPT is
|
|
|
|
not successfully opened
|
|
|
|
"""
|
2014-07-15 09:45:23 +00:00
|
|
|
# GIVEN: A reset mock_os_isdir
|
|
|
|
self.mock_os_isdir.reset()
|
2013-11-07 20:33:55 +00:00
|
|
|
|
|
|
|
# WHEN: The temporary directory does not exist and OpenPPT returns unsuccessfully (-1)
|
2014-07-15 09:45:23 +00:00
|
|
|
with patch('openlp.plugins.presentations.lib.pptviewcontroller.os.makedirs') as mock_makedirs:
|
|
|
|
self.mock_os_isdir.return_value = False
|
|
|
|
self.mock_controller.process.OpenPPT.return_value = -1
|
|
|
|
instance = PptviewDocument(self.mock_controller, self.mock_presentation)
|
|
|
|
instance.file_path = 'test\path.ppt'
|
2014-09-08 21:48:49 +00:00
|
|
|
if is_win():
|
2014-07-15 09:45:23 +00:00
|
|
|
result = instance.load_presentation()
|
2013-11-07 20:33:55 +00:00
|
|
|
|
2014-07-15 09:50:52 +00:00
|
|
|
# THEN: The temp folder should be created and PptviewDocument.load_presentation should return False
|
|
|
|
mock_makedirs.assert_called_once_with(self.temp_folder)
|
2014-07-15 09:45:23 +00:00
|
|
|
self.assertFalse(result)
|
2014-07-14 14:15:08 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_create_titles_and_notes(self):
|
2014-07-14 14:15:08 +00:00
|
|
|
"""
|
|
|
|
Test PowerpointController.create_titles_and_notes
|
|
|
|
"""
|
|
|
|
# GIVEN: mocked PresentationController.save_titles_and_notes and a pptx file
|
|
|
|
doc = PptviewDocument(self.mock_controller, self.mock_presentation)
|
2017-09-17 19:43:15 +00:00
|
|
|
doc.file_path = Path(TEST_RESOURCES_PATH, 'presentations', 'test.pptx')
|
2014-07-14 14:15:08 +00:00
|
|
|
doc.save_titles_and_notes = MagicMock()
|
|
|
|
|
|
|
|
# WHEN reading the titles and notes
|
|
|
|
doc.create_titles_and_notes()
|
|
|
|
|
|
|
|
# THEN save_titles_and_notes should have been called once with empty arrays
|
|
|
|
doc.save_titles_and_notes.assert_called_once_with(['Test 1\n', '\n', 'Test 2\n', 'Test 4\n', 'Test 3\n'],
|
2014-07-15 09:23:45 +00:00
|
|
|
['Notes for slide 1', 'Inserted', 'Notes for slide 2',
|
|
|
|
'Notes \nfor slide 4', 'Notes for slide 3'])
|
2014-07-14 14:15:08 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_create_titles_and_notes_nonexistent_file(self):
|
2014-07-14 14:15:08 +00:00
|
|
|
"""
|
|
|
|
Test PowerpointController.create_titles_and_notes with nonexistent file
|
|
|
|
"""
|
|
|
|
# GIVEN: mocked PresentationController.save_titles_and_notes and an nonexistent file
|
|
|
|
with patch('builtins.open') as mocked_open, \
|
2017-09-17 19:43:15 +00:00
|
|
|
patch.object(Path, 'exists') as mocked_path_exists, \
|
2014-07-15 09:23:45 +00:00
|
|
|
patch('openlp.plugins.presentations.lib.presentationcontroller.check_directory_exists') as \
|
2014-07-14 14:15:08 +00:00
|
|
|
mocked_dir_exists:
|
2017-09-17 19:43:15 +00:00
|
|
|
mocked_path_exists.return_value = False
|
2014-07-14 14:15:08 +00:00
|
|
|
mocked_dir_exists.return_value = False
|
|
|
|
doc = PptviewDocument(self.mock_controller, self.mock_presentation)
|
2017-09-17 19:43:15 +00:00
|
|
|
doc.file_path = Path('Idontexist.pptx')
|
2014-07-14 14:15:08 +00:00
|
|
|
doc.save_titles_and_notes = MagicMock()
|
|
|
|
|
|
|
|
# WHEN: Reading the titles and notes
|
|
|
|
doc.create_titles_and_notes()
|
|
|
|
|
|
|
|
# THEN: File existens should have been checked, and not have been opened.
|
|
|
|
doc.save_titles_and_notes.assert_called_once_with(None, None)
|
2017-09-17 19:43:15 +00:00
|
|
|
mocked_path_exists.assert_called_with()
|
2014-07-14 14:15:08 +00:00
|
|
|
self.assertEqual(mocked_open.call_count, 0, 'There should be no calls to open a file.')
|
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_create_titles_and_notes_invalid_file(self):
|
2014-07-14 14:15:08 +00:00
|
|
|
"""
|
|
|
|
Test PowerpointController.create_titles_and_notes with invalid file
|
|
|
|
"""
|
|
|
|
# GIVEN: mocked PresentationController.save_titles_and_notes and an invalid file
|
|
|
|
with patch('builtins.open') as mocked_open, \
|
|
|
|
patch('openlp.plugins.presentations.lib.pptviewcontroller.zipfile.is_zipfile') as mocked_is_zf:
|
|
|
|
mocked_is_zf.return_value = False
|
|
|
|
mocked_open.filesize = 10
|
|
|
|
doc = PptviewDocument(self.mock_controller, self.mock_presentation)
|
2017-09-17 19:43:15 +00:00
|
|
|
doc.file_path = Path(TEST_RESOURCES_PATH, 'presentations', 'test.ppt')
|
2014-07-14 14:15:08 +00:00
|
|
|
doc.save_titles_and_notes = MagicMock()
|
|
|
|
|
|
|
|
# WHEN: reading the titles and notes
|
|
|
|
doc.create_titles_and_notes()
|
|
|
|
|
|
|
|
# THEN:
|
|
|
|
doc.save_titles_and_notes.assert_called_once_with(None, None)
|
|
|
|
self.assertEqual(mocked_is_zf.call_count, 1, 'is_zipfile should have been called once')
|