diff --git a/openlp/plugins/presentations/lib/impresscontroller.py b/openlp/plugins/presentations/lib/impresscontroller.py index cdd6d162c..9bd540718 100644 --- a/openlp/plugins/presentations/lib/impresscontroller.py +++ b/openlp/plugins/presentations/lib/impresscontroller.py @@ -249,10 +249,7 @@ class ImpressDocument(PresentationDocument): window = self.document.getCurrentController().getFrame().getContainerWindow() window.setVisible(False) self.presentation = self.document.getPresentation() - try: - self.presentation.Display = ScreenList().current['number'] + 1 - except: - self.presentation.Display = 1 + self.presentation.Display = ScreenList().current['number'] + 1 self.control = None self.create_thumbnails() self.create_titles_and_notes() @@ -456,23 +453,26 @@ class ImpressDocument(PresentationDocument): def __get_text_from_page(self, slide_no, text_type=TextType.SlideText): """ Return any text extracted from the presentation page. - - ``notes`` - A boolean. If set the method searches the notes of the slide. + ``slide_no`` + 1 based slide index + ``text_type`` + A TextType. Enumeration of the types of supported text """ text = '' - pages = self.document.getDrawPages() - page = pages.getByIndex(slide_no - 1) - if text_type==TextType.Notes: - page = page.getNotesPage() - for index in range(page.getCount()): - shape = page.getByIndex(index) - shapeType = shape.getShapeType() - if shape.supportsService("com.sun.star.drawing.Text"): - # if they requested title, make sure it is the title - if text_type!=TextType.Title or \ - shapeType == "com.sun.star.presentation.TitleTextShape": - text += shape.getString() + '\n' + if text_type >= TextType.Title and text_type <= TextType.Notes: + pages = self.document.getDrawPages() + if slide_no > 0 and slide_no <= pages.getCount(): + page = pages.getByIndex(slide_no - 1) + if text_type==TextType.Notes: + page = page.getNotesPage() + for index in range(page.getCount()): + shape = page.getByIndex(index) + shapeType = shape.getShapeType() + if shape.supportsService("com.sun.star.drawing.Text"): + # if they requested title, make sure it is the title + if text_type!=TextType.Title or \ + shapeType == "com.sun.star.presentation.TitleTextShape": + text += shape.getString() + '\n' return text def create_titles_and_notes(self): diff --git a/tests/functional/openlp_plugins/presentations/test_impresscontroller.py b/tests/functional/openlp_plugins/presentations/test_impresscontroller.py index 5447bc67b..ef0628a87 100644 --- a/tests/functional/openlp_plugins/presentations/test_impresscontroller.py +++ b/tests/functional/openlp_plugins/presentations/test_impresscontroller.py @@ -31,8 +31,8 @@ Functional tests to test the Impress class and related methods. """ from unittest import TestCase import os -from mock import MagicMock, patch -from openlp.plugins.presentations.lib.impresscontroller import ImpressController, ImpressDocument +from mock import MagicMock, patch, mock_open +from openlp.plugins.presentations.lib.impresscontroller import ImpressController, ImpressDocument, TextType TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'resources')) @@ -44,9 +44,8 @@ class TestLibModule(TestCase): self.file_name = os.path.join(TEST_PATH,"test.pptx") self.ppc = ImpressController(mocked_plugin) self.doc = ImpressDocument(self.ppc,self.file_name) - self.doc.presentation_deleted() - def verify_installation_test(self): + def verify_installation(self): """ Test the installation of ImpressViewer """ @@ -56,33 +55,106 @@ class TestLibModule(TestCase): # THEN: We should get back a True bool assert isInstalled is True, 'The result should be True' - def verify_loading_document_test(self): + def create_titles_and_notes_test(self): """ - Test loading a document - """ - # GIVEN: the filename - print(self.file_name) - # WHEN: loading the filename - self.doc = ImpressDocument(self.ppc,self.file_name) - self.doc.load_presentation() - result = self.doc.is_loaded() - # THEN: result should be true - assert result is True, 'The result should be True' - - def verify_titles_test(self): + Test ImpressDocument.create_titles_and_notes """ - Test reading the titles from Impress - """ - # GIVEN: - self.doc = ImpressDocument(self.ppc,self.file_name) - self.doc.load_presentation() - #assert self.doc.is_loaded(), 'The document should have loaded' - self.doc.create_titles_and_notes() - #self.doc.load_presentation() + # GIVEN: mocked PresentationController.save_titles_and_notes with 0 pages and the LibreoOffice Document + 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 - titles,notes = self.doc.get_titles_and_notes() - print("titles: ".join(titles)) - print("notes: ".join(notes)) - # THEN there should be exactly 5 titles and 5 notes - assert len(titles)==5, 'There should be five titles' - assert len(notes)==5, 'Theres should be five notes' \ No newline at end of file + self.doc.create_titles_and_notes() + # THEN save_titles_and_notes should have been called once with empty arrays + self.doc.save_titles_and_notes.assert_called_once_with([],[]) + # 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() + # 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(['\n','\n'],[' ',' ']) + + def _mock_a_LibreOffice_document(self,pageCount,noteCount,textCount): + pages = MagicMock() + page = MagicMock() + pages.getByIndex.return_value = page + notesPage = MagicMock() + notesPage.getCount.return_value = noteCount + shape = MagicMock() + shape.supportsService.return_value = True + shape.getString.return_value = 'Note' + notesPage.getByIndex.return_value = shape + page.getNotesPage.return_value = notesPage + page.getCount.return_value = textCount + #pageShape.getString.return_value = 'Title' + #pageShape.getString.side_effect = self._get_string_side_effect + #page.getByIndex.return_value = pageShape + page.getByIndex.side_effect = self._get_page_shape_side_effect + pages.getCount.return_value = pageCount + document = MagicMock() + document.getDrawPages.return_value = pages + document.getByIndex.return_value = page + return document + + def _get_page_shape_side_effect(*args, **kwargs): + pageShape = MagicMock() + pageShape.supportsService.return_value = True + if args[1] == 0: + pageShape.getShapeType.return_value = 'com.sun.star.presentation.TitleTextShape' + pageShape.getString.return_value = 'Title' + else: + pageShape.getString.return_value = 'String' + return pageShape + + def get_text_from_page_out_of_bound_test(self): + """ + Test ImpressDocument.__get_text_from_page with out-of-bounds index + """ + # GIVEN: mocked LibreoOffice Document with one slide, two notes and three texts + self.doc.document = self._mock_a_LibreOffice_document(1,2,3) + # WHEN: __get_text_from_page is called with an index of 0x00 (index is 1 based) + result = self.doc._ImpressDocument__get_text_from_page(0,TextType.Notes) + # THEN: the result should be an empty string + assert result == '', 'Result should be an empty string' + # WHEN: regardless of the type of text, index 0x00 is out of bounds + result = self.doc._ImpressDocument__get_text_from_page(0,TextType.Title) + # THEN: result should be an empty string + assert result == '', 'Result should be an empty string' + # WHEN: when called with 2, it should also be out of bounds + result = self.doc._ImpressDocument__get_text_from_page(2,TextType.SlideText) + # THEN: result should be an empty string ... and, getByIndex should have never been called + assert result == '', 'Result should be an empty string' + assert self.doc.document.getDrawPages().getByIndex.call_count == 0, 'There should be no call to getByIndex' + + def get_text_from_page_wrong_type_test(self): + """ + Test ImpressDocument.__get_text_from_page with wrong TextType + """ + # GIVEN: mocked LibreOffice Document with one slide, two notes and three texts + self.doc.document = self._mock_a_LibreOffice_document(1,2,3) + # WHEN: called with TextType 3 + result = self.doc._ImpressDocument__get_text_from_page(1,3) + # THEN: result should be an empty string + assert result == '', 'Result should be and empty string' + assert self.doc.document.getDrawPages().getByIndex.call_count == 0, 'Theres should be no call to getByIndex' + + def get_text_from_page_valid_params_test(self): + """ + Test ImpressDocument.__get_text_from_page with valid parameters + """ + # GIVEN: mocked LibreOffice Document with one slide, two notes and three texts + self.doc.document = self._mock_a_LibreOffice_document(1,2,3) + # WHEN: __get_text_from_page is called to get the Notes + result = self.doc._ImpressDocument__get_text_from_page(1,TextType.Notes) + # THEN: result should be 'Note\nNote\n' + assert result == 'Note\nNote\n', 'Result should be exactly \'Note\\n\' times the count of notes in the page' + # WHEN: get the Title + result = self.doc._ImpressDocument__get_text_from_page(1,TextType.Title) + # THEN: result should be 'Title\n' + assert result == 'Title\n', 'Result should be exactly \'Title\\n\'' + # WHEN: get all text + result = self.doc._ImpressDocument__get_text_from_page(1,TextType.SlideText) + # THEN: result should be 'Title\nString\nString\n' + assert result == 'Title\nString\nString\n', 'Result should be exactly \'Title\\nString\\nString\\n\''