openlp/tests/openlp_plugins/presentations/test_pdfcontroller.py

124 lines
4.9 KiB
Python
Raw Normal View History

2013-12-29 19:46:04 +00:00
# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
2022-02-06 09:10:17 +00:00
# Copyright (c) 2008-2022 OpenLP Developers #
2019-04-13 13:00:22 +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, either version 3 of the License, or #
# (at your option) any later version. #
# #
# 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, see <https://www.gnu.org/licenses/>. #
##########################################################################
2013-12-29 19:46:04 +00:00
"""
This module contains tests for the PdfController
"""
import os
import pytest
from pathlib import Path
2020-09-26 23:40:29 +00:00
from shutil import rmtree
2014-03-14 22:08:44 +00:00
from tempfile import mkdtemp
2020-09-26 23:40:29 +00:00
from unittest.mock import MagicMock
2015-11-07 00:49:40 +00:00
from PyQt5 import QtCore, QtGui
2013-12-29 19:46:04 +00:00
2017-10-10 07:08:44 +00:00
from openlp.core.display.screens import ScreenList
2017-12-28 08:27:44 +00:00
from openlp.plugins.presentations.lib.pdfcontroller import PdfController, PdfDocument
2018-10-02 04:39:42 +00:00
from tests.utils.constants import RESOURCE_PATH
2013-12-29 19:46:04 +00:00
@pytest.fixture()
def pdf_env(settings, mock_plugin, mocked_qapp):
2020-03-05 20:34:08 +00:00
temp_folder_path = Path(mkdtemp())
thumbnail_folder_path = Path(mkdtemp())
mocked_screen = MagicMock()
mocked_screen.geometry.return_value = QtCore.QRect(0, 0, 1024, 768)
mocked_qapp.screens.return_value = [mocked_screen]
mocked_qapp.primaryScreen = MagicMock()
mocked_qapp.primaryScreen.return_value = mocked_screen
ScreenList.create(mocked_qapp)
yield mock_plugin, temp_folder_path, thumbnail_folder_path
2020-03-05 20:34:08 +00:00
rmtree(thumbnail_folder_path)
rmtree(temp_folder_path)
2013-12-29 19:46:04 +00:00
2020-03-05 20:34:08 +00:00
def test_constructor(settings, mock_plugin):
2013-12-29 19:46:04 +00:00
"""
2020-03-05 20:34:08 +00:00
Test the Constructor from the PdfController
2013-12-29 19:46:04 +00:00
"""
2020-03-05 20:34:08 +00:00
# GIVEN: No presentation controller
controller = None
# WHEN: The presentation controller object is created
controller = PdfController(plugin=mock_plugin)
# THEN: The name of the presentation controller should be correct
assert 'Pdf' == controller.name, 'The name of the presentation controller should be correct'
2020-09-26 23:40:29 +00:00
def load_pdf(pdf_env):
2020-03-05 20:34:08 +00:00
"""
Test loading a Pdf using the PdfController
"""
# GIVEN: A Pdf-file
test_file_path = RESOURCE_PATH / 'presentations' / 'pdf_test1.pdf'
# WHEN: The Pdf is loaded
mock_plugin = pdf_env[0]
temp_folder_path = pdf_env[1]
thumbnail_folder_path = pdf_env[2]
2020-03-05 20:34:08 +00:00
controller = PdfController(plugin=mock_plugin)
controller.temp_folder = temp_folder_path
controller.thumbnail_folder = thumbnail_folder_path
document = PdfDocument(controller, test_file_path)
loaded = document.load_presentation()
# THEN: The load should succeed and we should be able to get a pagecount
assert loaded is True, 'The loading of the PDF should succeed.'
assert 3 == document.get_slide_count(), 'The pagecount of the PDF should be 3.'
2020-09-26 23:40:29 +00:00
def load_pdf_pictures(pdf_env):
2020-03-05 20:34:08 +00:00
"""
Test loading a Pdf and check the generated pictures' size
"""
# GIVEN: A Pdf-file
test_file_path = RESOURCE_PATH / 'presentations' / 'pdf_test1.pdf'
# WHEN: The Pdf is loaded
mock_plugin = pdf_env[0]
temp_folder_path = pdf_env[1]
thumbnail_folder_path = pdf_env[2]
2020-03-05 20:34:08 +00:00
controller = PdfController(plugin=mock_plugin)
controller.temp_folder = temp_folder_path
controller.thumbnail_folder = thumbnail_folder_path
document = PdfDocument(controller, test_file_path)
loaded = document.load_presentation()
# THEN: The load should succeed and pictures should be created and have been scaled to fit the screen
assert loaded is True, 'The loading of the PDF should succeed.'
image = QtGui.QImage(os.path.join(str(temp_folder_path), 'pdf_test1.pdf', 'mainslide001.png'))
2020-09-26 23:40:29 +00:00
# Calculate the width of the PDF based on the aspect ratio of the PDF
height = 768
2020-09-26 23:40:29 +00:00
width = int(round(height * 0.70703125, 0))
assert image.height() == height, 'The height should be {height}'.format(height=height)
assert image.width() == width, 'The width should be {width}'.format(width=width)
2020-03-05 20:34:08 +00:00
2020-03-08 21:45:42 +00:00
def test_loading_pdf_using_pymupdf(pdf_env):
2020-03-05 20:34:08 +00:00
try:
import fitz # noqa: F401
except ImportError:
pytest.skip('PyMuPDF is not installed')
2020-09-26 23:40:29 +00:00
load_pdf(pdf_env)
load_pdf_pictures(pdf_env)