1
# -*- coding: utf-8 -*-
2
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
4
###############################################################################
5
# OpenLP - Open Source Lyrics Projection #
6
# --------------------------------------------------------------------------- #
7
# Copyright (c) 2008-2019 OpenLP Developers #
8
# --------------------------------------------------------------------------- #
9
# This program is free software; you can redistribute it and/or modify it #
10
# under the terms of the GNU General Public License as published by the Free #
11
# Software Foundation; version 2 of the License. #
13
# This program is distributed in the hope that it will be useful, but WITHOUT #
14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
15
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
18
# You should have received a copy of the GNU General Public License along #
19
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
20
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
21
###############################################################################
23
This module contains tests for the pptviewcontroller module of the Presentations plugin.
26
from tempfile import mkdtemp
27
from unittest import TestCase, skipIf
28
from unittest.mock import MagicMock, patch
30
from openlp.core.common import is_win
31
from openlp.core.common.path import Path
32
from openlp.plugins.presentations.lib.pptviewcontroller import PptviewDocument, PptviewController
33
from tests.helpers.testmixin import TestMixin
34
from tests.utils.constants import RESOURCE_PATH
37
class TestPptviewController(TestCase, TestMixin):
39
Test the PptviewController Class
43
Set up the patches and mocks need for all tests.
45
self.setup_application()
47
self.mock_plugin = MagicMock()
48
self.temp_folder = mkdtemp()
49
self.mock_plugin.settings_section = self.temp_folder
55
self.destroy_settings()
56
shutil.rmtree(self.temp_folder)
58
def test_constructor(self):
60
Test the Constructor from the PptViewController
62
# GIVEN: No presentation controller
65
# WHEN: The presentation controller object is created
66
controller = PptviewController(plugin=self.mock_plugin)
68
# THEN: The name of the presentation controller should be correct
69
assert 'Powerpoint Viewer' == controller.name, 'The name of the presentation controller should be correct'
71
@skipIf(not is_win(), 'Not Windows')
72
@patch('openlp.plugins.presentations.lib.pptviewcontroller.cdll.LoadLibrary')
73
def test_check_available(self, mocked_load_library):
75
Test check_available / check_installed
77
# GIVEN: A mocked dll loader and a controller
78
mocked_process = MagicMock()
79
mocked_process.CheckInstalled.return_value = True
80
mocked_load_library.return_value = mocked_process
81
controller = PptviewController(plugin=self.mock_plugin)
83
# WHEN: check_available is called
84
available = controller.check_available()
86
# THEN: On windows it should return True, on other platforms False
87
assert available is True, 'check_available should return True on windows.'
90
class TestPptviewDocument(TestCase):
92
Test the PptviewDocument Class
96
Set up the patches and mocks need for all tests.
98
self.pptview_document_create_thumbnails_patcher = patch(
99
'openlp.plugins.presentations.lib.pptviewcontroller.PptviewDocument.create_thumbnails')
100
self.pptview_document_stop_presentation_patcher = patch(
101
'openlp.plugins.presentations.lib.pptviewcontroller.PptviewDocument.stop_presentation')
102
self.presentation_document_get_temp_folder_patcher = patch(
103
'openlp.plugins.presentations.lib.pptviewcontroller.PresentationDocument.get_temp_folder')
104
self.presentation_document_setup_patcher = patch(
105
'openlp.plugins.presentations.lib.pptviewcontroller.PresentationDocument._setup')
106
self.screen_list_patcher = patch('openlp.plugins.presentations.lib.pptviewcontroller.ScreenList')
107
self.rect_patcher = MagicMock()
108
self.mock_pptview_document_create_thumbnails = self.pptview_document_create_thumbnails_patcher.start()
109
self.mock_pptview_document_stop_presentation = self.pptview_document_stop_presentation_patcher.start()
110
self.mock_presentation_document_get_temp_folder = self.presentation_document_get_temp_folder_patcher.start()
111
self.mock_presentation_document_setup = self.presentation_document_setup_patcher.start()
112
self.mock_rect = self.rect_patcher.start()
113
self.mock_screen_list = self.screen_list_patcher.start()
114
self.mock_controller = MagicMock()
115
self.mock_presentation = MagicMock()
116
self.temp_folder = mkdtemp()
117
self.mock_presentation_document_get_temp_folder.return_value = self.temp_folder
123
self.pptview_document_create_thumbnails_patcher.stop()
124
self.pptview_document_stop_presentation_patcher.stop()
125
self.presentation_document_get_temp_folder_patcher.stop()
126
self.presentation_document_setup_patcher.stop()
127
self.rect_patcher.stop()
128
self.screen_list_patcher.stop()
129
shutil.rmtree(self.temp_folder)
131
@skipIf(not is_win(), 'Not Windows')
132
def test_load_presentation_succesful(self):
134
Test the PptviewDocument.load_presentation() method when the PPT is successfully opened
136
# GIVEN: A reset mocked_os
137
self.mock_controller.process.OpenPPT.return_value = 0
138
instance = PptviewDocument(self.mock_controller, self.mock_presentation)
139
instance.file_path = 'test\path.ppt'
141
# WHEN: The temporary directory exists and OpenPPT returns successfully (not -1)
142
result = instance.load_presentation()
144
# THEN: PptviewDocument.load_presentation should return True
145
assert result is True
147
@skipIf(not is_win(), 'Not Windows')
148
def test_load_presentation_un_succesful(self):
150
Test the PptviewDocument.load_presentation() method when the temporary directory does not exist and the PPT is
151
not successfully opened
153
# GIVEN: A reset mock_os_isdir
154
self.mock_controller.process.OpenPPT.return_value = -1
155
instance = PptviewDocument(self.mock_controller, self.mock_presentation)
156
instance.file_path = 'test\path.ppt'
158
# WHEN: The temporary directory does not exist and OpenPPT returns unsuccessfully (-1)
159
with patch.object(instance, 'get_temp_folder') as mocked_get_folder:
160
mocked_get_folder.return_value = MagicMock(spec=Path)
161
result = instance.load_presentation()
163
# THEN: The temp folder should be created and PptviewDocument.load_presentation should return False
164
assert result is False
166
def test_create_titles_and_notes(self):
168
Test PowerpointController.create_titles_and_notes
170
# GIVEN: mocked PresentationController.save_titles_and_notes and a pptx file
171
doc = PptviewDocument(self.mock_controller, self.mock_presentation)
172
doc.file_path = RESOURCE_PATH / 'presentations' / 'test.pptx'
173
doc.save_titles_and_notes = MagicMock()
175
# WHEN reading the titles and notes
176
doc.create_titles_and_notes()
178
# THEN save_titles_and_notes should have been called once with empty arrays
179
doc.save_titles_and_notes.assert_called_once_with(['Test 1\n', '\n', 'Test 2\n', 'Test 4\n', 'Test 3\n'],
180
['Notes for slide 1', 'Inserted', 'Notes for slide 2',
181
'Notes \nfor slide 4', 'Notes for slide 3'])
183
def test_create_titles_and_notes_nonexistent_file(self):
185
Test PowerpointController.create_titles_and_notes with nonexistent file
187
# GIVEN: mocked PresentationController.save_titles_and_notes and an nonexistent file
188
with patch('builtins.open') as mocked_open, \
189
patch.object(Path, 'exists') as mocked_path_exists, \
190
patch('openlp.plugins.presentations.lib.presentationcontroller.create_paths') as \
192
mocked_path_exists.return_value = False
193
mocked_dir_exists.return_value = False
194
doc = PptviewDocument(self.mock_controller, self.mock_presentation)
195
doc.file_path = Path('Idontexist.pptx')
196
doc.save_titles_and_notes = MagicMock()
198
# WHEN: Reading the titles and notes
199
doc.create_titles_and_notes()
201
# THEN: File existens should have been checked, and not have been opened.
202
doc.save_titles_and_notes.assert_called_once_with(None, None)
203
mocked_path_exists.assert_called_with()
204
assert mocked_open.call_count == 0, 'There should be no calls to open a file.'
206
def test_create_titles_and_notes_invalid_file(self):
208
Test PowerpointController.create_titles_and_notes with invalid file
210
# GIVEN: mocked PresentationController.save_titles_and_notes and an invalid file
211
with patch('builtins.open') as mocked_open, \
212
patch('openlp.plugins.presentations.lib.pptviewcontroller.zipfile.is_zipfile') as mocked_is_zf:
213
mocked_is_zf.return_value = False
214
mocked_open.filesize = 10
215
doc = PptviewDocument(self.mock_controller, self.mock_presentation)
216
doc.file_path = RESOURCE_PATH / 'presentations' / 'test.ppt'
217
doc.save_titles_and_notes = MagicMock()
219
# WHEN: reading the titles and notes
220
doc.create_titles_and_notes()
223
doc.save_titles_and_notes.assert_called_once_with(None, None)
224
assert mocked_is_zf.call_count == 1, 'is_zipfile should have been called once'