From d08f488040473875de93d4a3ad239635274669de Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 26 Jan 2024 23:30:24 -0700 Subject: [PATCH] Fix a PermissionError that occurs on Windows 10/11 when qtawesome tries to look at its own fonts --- openlp/core/ui/icons.py | 5 ++-- tests/openlp_core/ui/test_icons.py | 37 ++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/openlp/core/ui/icons.py b/openlp/core/ui/icons.py index 00fc12368..98d8cc02f 100644 --- a/openlp/core/ui/icons.py +++ b/openlp/core/ui/icons.py @@ -44,9 +44,8 @@ class UiIcons(metaclass=Singleton): """ These are the font icons used in the code. """ - font_path = AppLocation.get_directory(AppLocation.AppDir) / 'core' / 'ui' / 'fonts' / 'OpenLP.ttf' - charmap_path = AppLocation.get_directory(AppLocation.AppDir) / 'core' / 'ui' / 'fonts' / 'openlp-charmap.json' - qta.load_font('op', font_path, charmap_path) + font_path = AppLocation.get_directory(AppLocation.AppDir) / 'core' / 'ui' / 'fonts' + qta.load_font('op', 'OpenLP.ttf', 'openlp-charmap.json', directory=str(font_path)) palette = QtWidgets.QApplication.palette() self._default_icon_colors = { "color": palette.color(QtGui.QPalette.ColorGroup.Active, QtGui.QPalette.ColorRole.WindowText), diff --git a/tests/openlp_core/ui/test_icons.py b/tests/openlp_core/ui/test_icons.py index 4bc990741..5ba0ff467 100644 --- a/tests/openlp_core/ui/test_icons.py +++ b/tests/openlp_core/ui/test_icons.py @@ -20,13 +20,18 @@ """ Package to test the openlp.core.ui.icons package. """ -from unittest.mock import patch +from pathlib import Path +from unittest.mock import MagicMock, patch + from PyQt5 import QtGui +from openlp.core.common import Singleton +from openlp.core.common.registry import Registry +from openlp.core.common.settings import Settings from openlp.core.ui.icons import UiIcons -def test_simple_icon(settings): +def test_simple_icon(settings: Settings): # GIVEN: an basic set of icons with patch('openlp.core.ui.icons.UiIcons.__init__', return_value=None): icons = UiIcons() @@ -39,3 +44,31 @@ def test_simple_icon(settings): icon_active = UiIcons().active # THEN: I should have an icon assert isinstance(icon_active, QtGui.QIcon) + + +@patch('openlp.core.ui.icons.qta') +@patch('openlp.core.ui.icons.build_icon') +@patch('openlp.core.ui.icons.UiIcons.load_icons') +@patch('openlp.core.ui.icons.AppLocation.get_directory') +def test_uiicons_init(mocked_get_directory: MagicMock, mocked_load_icons: MagicMock, mocked_build_icon: MagicMock, + mocked_qta: MagicMock, settings: Settings, registry: Registry, request): + """Test the __init__ method, that it makes the correct calls""" + # Set up test cleanup + def remove_singleton(): + del Singleton._instances[UiIcons] + + request.addfinalizer(remove_singleton) + + # GIVEN: A mocked out qta module, and no instances already created + remove_singleton() + mocked_get_directory.return_value = Path('openlp') + expected_dir = str(Path('openlp', 'core', 'ui', 'fonts')) + + # WHEN: A UiIcons instance is created + icons = UiIcons() + + # THEN: The mocks should have been called correctly + mocked_qta.load_font.assert_called_once_with('op', 'OpenLP.ttf', 'openlp-charmap.json', directory=expected_dir) + mocked_qta.set_defaults.assert_called_once_with(**icons._default_icon_colors) + mocked_load_icons.assert_called_once_with(icons._icon_list) + mocked_build_icon.assert_called_once_with(':/icon/openlp-logo.svg')