def test_start_presentation_no_control(mocked_sleep): """ Test the start_presentation() method when there's no control """ # GIVEN: A LibreOfficeServer instance and some mocks server = LibreOfficeServer() mocked_control = MagicMock() mocked_document = MagicMock() mocked_presentation = MagicMock() mocked_controller = MagicMock() mocked_frame = MagicMock() mocked_window = MagicMock() server._document = mocked_document server._presentation = mocked_presentation mocked_document.getCurrentController.return_value = mocked_controller mocked_controller.getFrame.return_value = mocked_frame mocked_frame.getContainerWindow.return_value = mocked_window mocked_presentation.getController.side_effect = [None, mocked_control] # WHEN: start_presentation() is called server.start_presentation() # THEN: The slide number should be correct mocked_document.getCurrentController.assert_called_once_with() mocked_controller.getFrame.assert_called_once_with() mocked_frame.getContainerWindow.assert_called_once_with() mocked_presentation.start.assert_called_once_with() assert mocked_presentation.getController.call_count == 2 mocked_sleep.assert_called_once_with(0.1) assert mocked_window.setVisible.call_args_list == [call(True), call(False)] assert server._control is mocked_control
def test_get_titles_and_notes(): """ Test the get_titles_and_notes() method """ # GIVEN: A LibreOfficeServer object and a bunch of mocks server = LibreOfficeServer() mocked_document = MagicMock() mocked_pages = MagicMock() server._document = mocked_document mocked_document.getDrawPages.return_value = mocked_pages mocked_pages.getCount.return_value = 2 # WHEN: get_titles_and_notes() is called with patch.object(server, '_get_text_from_page') as mocked_get_text_from_page: mocked_get_text_from_page.side_effect = [ 'OpenLP on Mac OS X', '', '', 'Installing is a drag-and-drop affair' ] titles, notes = server.get_titles_and_notes() # THEN: The right calls are made and the right stuff returned mocked_document.getDrawPages.assert_called_once_with() mocked_pages.getCount.assert_called_once_with() assert mocked_get_text_from_page.call_count == 4 expected_calls = [ call(1, TextType.Title), call(1, TextType.Notes), call(2, TextType.Title), call(2, TextType.Notes), ] assert mocked_get_text_from_page.call_args_list == expected_calls assert titles == ['OpenLP on Mac OS X\n', '\n'], titles assert notes == [' ', 'Installing is a drag-and-drop affair'], notes
def test_get_text_from_page_notes(): """ Test that the _get_text_from_page() method gives us the text from the notes """ # GIVEN: A LibreOfficeServer object and some mocked objects text_type = TextType.Notes slide_no = 1 server = LibreOfficeServer() server._document = MagicMock() mocked_pages = MagicMock() mocked_page = MagicMock() mocked_notes_page = MagicMock() mocked_shape = MagicMock() server._document.getDrawPages.return_value = mocked_pages mocked_pages.getCount.return_value = 1 mocked_pages.getByIndex.return_value = mocked_page mocked_page.getNotesPage.return_value = mocked_notes_page mocked_notes_page.getByIndex.return_value = mocked_shape mocked_shape.getShapeType.return_value = 'com.sun.star.presentation.TitleTextShape' mocked_shape.supportsService.return_value = True mocked_shape.getString.return_value = 'Page Notes' # WHEN: _get_text_from_page() is run for titles text = server._get_text_from_page(slide_no, text_type) # THEN: The text should be correct assert text == 'Page Notes\n'
def test_get_slide_count(): """ Test the get_slide_count() method """ # GIVEN: A LibreOfficeServer instance and some mocks server = LibreOfficeServer() mocked_document = MagicMock() mocked_pages = MagicMock() server._document = mocked_document mocked_document.getDrawPages.return_value = mocked_pages mocked_pages.getCount.return_value = 2 # WHEN: get_slide_count() is called result = server.get_slide_count() # THEN: The slide count should be correct assert result == 2
def test_is_loaded_exception(): """ Test the is_loaded() method when an exception is thrown """ # GIVEN: A LibreOfficeServer instance and a bunch of mocks server = LibreOfficeServer() mocked_document = MagicMock() server._document = mocked_document server._presentation = MagicMock() mocked_document.getPresentation.side_effect = Exception() # WHEN: The is_loaded() method is called result = server.is_loaded() # THEN: The result should be false assert result is False mocked_document.getPresentation.assert_called_once_with()
def test_is_loaded_no_presentation(): """ Test the is_loaded() method when there's no presentation """ # GIVEN: A LibreOfficeServer instance and a bunch of mocks server = LibreOfficeServer() mocked_document = MagicMock() server._document = mocked_document server._presentation = MagicMock() mocked_document.getPresentation.return_value = None # WHEN: The is_loaded() method is called result = server.is_loaded() # THEN: The result should be false assert result is False mocked_document.getPresentation.assert_called_once_with()
def test_close_presentation(): """ Test that closing the presentation cleans things up correctly """ # GIVEN: A LibreOfficeServer instance and a bunch of mocks server = LibreOfficeServer() mocked_document = MagicMock() mocked_presentation = MagicMock() server._document = mocked_document server._presentation = mocked_presentation # WHEN: close_presentation() is called server.close_presentation() # THEN: The presentation and document should be closed mocked_presentation.end.assert_called_once_with() mocked_document.dispose.assert_called_once_with() assert server._document is None assert server._presentation is None
def test_extract_thumbnails_no_pages(mocked_uno): """ Test the extract_thumbnails() method when there are no pages """ # GIVEN: A LibreOfficeServer instance temp_folder = '/tmp' server = LibreOfficeServer() mocked_document = MagicMock() server._document = mocked_document mocked_uno.systemPathToFileUrl.side_effect = lambda x: x mocked_document.getDrawPages.return_value = None # WHEN: The extract_thumbnails() method is called with patch.object(server, '_create_property') as mocked_create_property: mocked_create_property.side_effect = lambda x, y: {x: y} thumbnails = server.extract_thumbnails(temp_folder) # THEN: Thumbnails have been extracted mocked_uno.systemPathToFileUrl.assert_called_once_with(temp_folder) mocked_create_property.assert_called_once_with('FilterName', 'impress_png_Export') mocked_document.getDrawPages.assert_called_once_with() assert thumbnails == []
def test_extract_thumbnails(mocked_os, mocked_uno): """ Test the extract_thumbnails() method """ # GIVEN: A LibreOfficeServer instance temp_folder = '/tmp' server = LibreOfficeServer() mocked_document = MagicMock() mocked_pages = MagicMock() mocked_page_1 = MagicMock() mocked_page_2 = MagicMock() mocked_controller = MagicMock() server._document = mocked_document mocked_uno.systemPathToFileUrl.side_effect = lambda x: x mocked_document.getDrawPages.return_value = mocked_pages mocked_os.path.isdir.return_value = False mocked_pages.getCount.return_value = 2 mocked_pages.getByIndex.side_effect = [mocked_page_1, mocked_page_2] mocked_document.getCurrentController.return_value = mocked_controller mocked_os.path.join.side_effect = lambda *x: '/'.join(x) # WHEN: The extract_thumbnails() method is called with patch.object(server, '_create_property') as mocked_create_property: mocked_create_property.side_effect = lambda x, y: {x: y} thumbnails = server.extract_thumbnails(temp_folder) # THEN: Thumbnails have been extracted mocked_uno.systemPathToFileUrl.assert_called_once_with(temp_folder) mocked_create_property.assert_called_once_with('FilterName', 'impress_png_Export') mocked_document.getDrawPages.assert_called_once_with() mocked_pages.getCount.assert_called_once_with() assert mocked_pages.getByIndex.call_args_list == [call(0), call(1)] assert mocked_controller.setCurrentPage.call_args_list == \ [call(mocked_page_1), call(mocked_page_2)] assert mocked_document.storeToURL.call_args_list == \ [call('/tmp/1.png', ({'FilterName': 'impress_png_Export'},)), call('/tmp/2.png', ({'FilterName': 'impress_png_Export'},))] assert thumbnails == ['/tmp/1.png', '/tmp/2.png']