test_pdfcontroller: Get rid of platform specifics

As system() called PDF controllers are removed, test_pdfcontroller can be
simplified because PyMuPDF will use the mocked screen.
Get rid of the platform-dependent screen size obtaining which gets rid of
the Xlib dependency.
This commit is contained in:
Bastian Germann 2020-09-28 21:47:35 +02:00
parent 8c888b9a00
commit e09aec5d34
3 changed files with 8 additions and 44 deletions

View File

@ -58,7 +58,6 @@ LINUX_MODULES = [
# Optical drive detection. # Optical drive detection.
'dbus', 'dbus',
'distro', 'distro',
'Xlib',
] ]
MACOSX_MODULES = [ MACOSX_MODULES = [

View File

@ -138,7 +138,6 @@ using a computer and a data projector.""",
'pysword', 'pysword',
'pytest', 'pytest',
'pytest-qt', 'pytest-qt',
'python-xlib; platform_system=="Linux"',
'flake8', 'flake8',
] ]
}, },

View File

@ -26,12 +26,10 @@ import pytest
from pathlib import Path from pathlib import Path
from shutil import rmtree from shutil import rmtree
from tempfile import mkdtemp from tempfile import mkdtemp
from unittest import skipIf
from unittest.mock import MagicMock from unittest.mock import MagicMock
from PyQt5 import QtCore, QtGui from PyQt5 import QtCore, QtGui
from openlp.core.common import is_macosx, is_linux, is_win
from openlp.core.display.screens import ScreenList from openlp.core.display.screens import ScreenList
from openlp.plugins.presentations.lib.pdfcontroller import PdfController, PdfDocument from openlp.plugins.presentations.lib.pdfcontroller import PdfController, PdfDocument
from tests.utils.constants import RESOURCE_PATH from tests.utils.constants import RESOURCE_PATH
@ -47,41 +45,11 @@ def pdf_env(settings, mock_plugin, mocked_qapp):
mocked_qapp.primaryScreen = MagicMock() mocked_qapp.primaryScreen = MagicMock()
mocked_qapp.primaryScreen.return_value = mocked_screen mocked_qapp.primaryScreen.return_value = mocked_screen
ScreenList.create(mocked_qapp) ScreenList.create(mocked_qapp)
yield settings, mock_plugin, temp_folder_path, thumbnail_folder_path yield mock_plugin, temp_folder_path, thumbnail_folder_path
rmtree(thumbnail_folder_path) rmtree(thumbnail_folder_path)
rmtree(temp_folder_path) rmtree(temp_folder_path)
SCREEN = {
'primary': False,
'number': 1,
'size': QtCore.QRect(0, 0, 1024, 768)
}
IS_CI = 'GITLAB_CI' in os.environ or 'APPVEYOR' in os.environ
IS_QT_QPA_PLATFORM_OFFSCREEN = 'QT_QPA_PLATFORM' in os.environ and os.environ['QT_QPA_PLATFORM'] == 'offscreen'
def get_screen_resolution():
"""
Get the screen resolution
"""
if is_macosx():
# Magic numbers... don't ask me why
return 1024, 768
elif is_win():
from win32api import GetSystemMetrics
return GetSystemMetrics(0), GetSystemMetrics(1)
elif is_linux():
if IS_CI:
return 1024, 768
else:
from Xlib.display import Display
resolution = Display().screen().root.get_geometry()
return resolution.width, resolution.height
else:
return 1024, 768
def test_constructor(settings, mock_plugin): def test_constructor(settings, mock_plugin):
""" """
Test the Constructor from the PdfController Test the Constructor from the PdfController
@ -104,9 +72,9 @@ def load_pdf(pdf_env):
test_file_path = RESOURCE_PATH / 'presentations' / 'pdf_test1.pdf' test_file_path = RESOURCE_PATH / 'presentations' / 'pdf_test1.pdf'
# WHEN: The Pdf is loaded # WHEN: The Pdf is loaded
mock_plugin = pdf_env[1] mock_plugin = pdf_env[0]
temp_folder_path = pdf_env[2] temp_folder_path = pdf_env[1]
thumbnail_folder_path = pdf_env[3] thumbnail_folder_path = pdf_env[2]
controller = PdfController(plugin=mock_plugin) controller = PdfController(plugin=mock_plugin)
controller.temp_folder = temp_folder_path controller.temp_folder = temp_folder_path
controller.thumbnail_folder = thumbnail_folder_path controller.thumbnail_folder = thumbnail_folder_path
@ -118,7 +86,6 @@ def load_pdf(pdf_env):
assert 3 == document.get_slide_count(), 'The pagecount of the PDF should be 3.' assert 3 == document.get_slide_count(), 'The pagecount of the PDF should be 3.'
@skipIf(IS_QT_QPA_PLATFORM_OFFSCREEN, 'This test fails when QT_QPA_PLATFORM is set to "offscreen".')
def load_pdf_pictures(pdf_env): def load_pdf_pictures(pdf_env):
""" """
Test loading a Pdf and check the generated pictures' size Test loading a Pdf and check the generated pictures' size
@ -127,9 +94,9 @@ def load_pdf_pictures(pdf_env):
test_file_path = RESOURCE_PATH / 'presentations' / 'pdf_test1.pdf' test_file_path = RESOURCE_PATH / 'presentations' / 'pdf_test1.pdf'
# WHEN: The Pdf is loaded # WHEN: The Pdf is loaded
mock_plugin = pdf_env[1] mock_plugin = pdf_env[0]
temp_folder_path = pdf_env[2] temp_folder_path = pdf_env[1]
thumbnail_folder_path = pdf_env[3] thumbnail_folder_path = pdf_env[2]
controller = PdfController(plugin=mock_plugin) controller = PdfController(plugin=mock_plugin)
controller.temp_folder = temp_folder_path controller.temp_folder = temp_folder_path
controller.thumbnail_folder = thumbnail_folder_path controller.thumbnail_folder = thumbnail_folder_path
@ -139,9 +106,8 @@ def load_pdf_pictures(pdf_env):
# THEN: The load should succeed and pictures should be created and have been scaled to fit the screen # 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.' 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')) image = QtGui.QImage(os.path.join(str(temp_folder_path), 'pdf_test1.pdf', 'mainslide001.png'))
# Based on the converter used the resolution will differ a bit
width, height = get_screen_resolution()
# Calculate the width of the PDF based on the aspect ratio of the PDF # Calculate the width of the PDF based on the aspect ratio of the PDF
height = 768
width = int(round(height * 0.70703125, 0)) width = int(round(height * 0.70703125, 0))
assert image.height() == height, 'The height should be {height}'.format(height=height) assert image.height() == height, 'The height should be {height}'.format(height=height)
assert image.width() == width, 'The width should be {width}'.format(width=width) assert image.width() == width, 'The width should be {width}'.format(width=width)