diff --git a/openlp/core/ui/mainwindow.py b/openlp/core/ui/mainwindow.py index 81830f287..16ca79072 100644 --- a/openlp/core/ui/mainwindow.py +++ b/openlp/core/ui/mainwindow.py @@ -670,6 +670,10 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, LogMixin, RegistryPropert self.application.process_events() plugin.first_time() self.application.process_events() + # Load the themes from files + self.theme_manager_contents.load_first_time_themes() + # Update the theme widget + self.theme_manager_contents.load_themes() temp_path = Path(gettempdir(), 'openlp') shutil.rmtree(temp_path, True) @@ -714,10 +718,6 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, LogMixin, RegistryPropert self.active_plugin.toggle_status(PluginStatus.Inactive) # Set global theme and Registry().execute('theme_update_global') - # Load the themes from files - self.theme_manager_contents.load_first_time_themes() - # Update the theme widget - self.theme_manager_contents.load_themes() # Check if any Bibles downloaded. If there are, they will be processed. Registry().execute('bibles_load_list') self.application.set_normal_cursor() diff --git a/openlp/core/ui/thememanager.py b/openlp/core/ui/thememanager.py index 2a447b7a9..8539f1eab 100644 --- a/openlp/core/ui/thememanager.py +++ b/openlp/core/ui/thememanager.py @@ -149,16 +149,15 @@ class ThemeManager(QtWidgets.QWidget, RegistryBase, Ui_ThemeManager, LogMixin, R process the bootstrap initialise setup request """ self.setup_ui(self) - self.progress_form = ThemeProgressForm(self) self.global_theme = Settings().value(self.settings_section + '/global theme') self.build_theme_path() - self.load_first_time_themes() self.upgrade_themes() # TODO: Can be removed when upgrade path from OpenLP 2.4 no longer needed def bootstrap_post_set_up(self): """ process the bootstrap post setup request """ + self.progress_form = ThemeProgressForm(self) self.theme_form = ThemeForm(self) self.theme_form.path = self.theme_path self.file_rename_form = FileRenameForm() @@ -481,7 +480,8 @@ class ThemeManager(QtWidgets.QWidget, RegistryBase, Ui_ThemeManager, LogMixin, R self.save_theme(theme) Settings().setValue(self.settings_section + '/global theme', theme.theme_name) new_themes = [theme.theme_name] - self.update_preview_images(new_themes) + if new_themes: + self.update_preview_images(new_themes) self.application.set_normal_cursor() def load_themes(self): diff --git a/openlp/core/ui/themeprogressform.py b/openlp/core/ui/themeprogressform.py index 4d61357a3..c636dd834 100644 --- a/openlp/core/ui/themeprogressform.py +++ b/openlp/core/ui/themeprogressform.py @@ -36,6 +36,8 @@ class ThemeProgressForm(QtWidgets.QDialog, UiThemeProgressDialog, RegistryProper super().__init__(parent) self.setup_ui(self) self._theme_list = [] + + def show(self): self.progress_bar.setMinimum(0) self.progress_bar.setMaximum(0) self.progress_bar.setValue(0) @@ -45,6 +47,7 @@ class ThemeProgressForm(QtWidgets.QDialog, UiThemeProgressDialog, RegistryProper except ZeroDivisionError: self.ratio = 16 / 9 self.theme_preview_layout.aspect_ratio = self.ratio + return super().show() def get_preview(self, theme_name, theme_data): self.label.setText(theme_name) diff --git a/tests/functional/openlp_core/display/test_window.py b/tests/functional/openlp_core/display/test_window.py index c2c1990d0..df1b8aded 100644 --- a/tests/functional/openlp_core/display/test_window.py +++ b/tests/functional/openlp_core/display/test_window.py @@ -33,22 +33,24 @@ sys.modules['PyQt5.QtWebEngineWidgets'] = MagicMock() from openlp.core.display.window import DisplayWindow from tests.helpers.testmixin import TestMixin -from openlp.core.common.settings import Settings @patch('PyQt5.QtWidgets.QVBoxLayout') @patch('openlp.core.display.webengine.WebEngineView') +@patch('openlp.core.display.window.Settings') class TestDisplayWindow(TestCase, TestMixin): """ A test suite to test the functions in DisplayWindow """ - def test_x11_override_on(self, mocked_webengine, mocked_addWidget): + def test_x11_override_on(self, MockSettings, mocked_webengine, mocked_addWidget): """ Test that the x11 override option bit is set """ # GIVEN: x11 bypass is on - Settings().setValue('advanced/x11 bypass wm', True) + mocked_settings = MagicMock() + mocked_settings.value.return_value = True + MockSettings.return_value = mocked_settings # WHEN: A DisplayWindow is generated display_window = DisplayWindow() @@ -57,12 +59,14 @@ class TestDisplayWindow(TestCase, TestMixin): x11_bit = display_window.windowFlags() & QtCore.Qt.X11BypassWindowManagerHint assert x11_bit == QtCore.Qt.X11BypassWindowManagerHint - def test_x11_override_off(self, mocked_webengine, mocked_addWidget): + def test_x11_override_off(self, MockSettings, mocked_webengine, mocked_addWidget): """ Test that the x11 override option bit is not set when setting if off """ # GIVEN: x11 bypass is off - Settings().setValue('advanced/x11 bypass wm', False) + mocked_settings = MagicMock() + mocked_settings.value.return_value = False + MockSettings.return_value = mocked_settings # WHEN: A DisplayWindow is generated display_window = DisplayWindow() diff --git a/tests/functional/openlp_core/ui/test_themeform.py b/tests/functional/openlp_core/ui/test_themeform.py index c05b9a2b7..bbb205f0a 100644 --- a/tests/functional/openlp_core/ui/test_themeform.py +++ b/tests/functional/openlp_core/ui/test_themeform.py @@ -28,9 +28,9 @@ from unittest.mock import MagicMock, patch from openlp.core.ui.themeform import ThemeForm -class TestThemeManager(TestCase): +class TestThemeForm(TestCase): """ - Test the functions in the ThemeManager Class + Test the functions in the ThemeForm Class """ def setUp(self): with patch('openlp.core.ui.themeform.ThemeForm._setup'): diff --git a/tests/interfaces/openlp_core/ui/test_thememanager.py b/tests/interfaces/openlp_core/ui/test_thememanager.py index a5b5a88e5..df47d1d81 100644 --- a/tests/interfaces/openlp_core/ui/test_thememanager.py +++ b/tests/interfaces/openlp_core/ui/test_thememanager.py @@ -58,19 +58,16 @@ class TestThemeManager(TestCase, TestMixin): # GIVEN: A new a call to initialise self.theme_manager.setup_ui = MagicMock() self.theme_manager.build_theme_path = MagicMock() - self.theme_manager.load_first_time_themes = MagicMock() self.theme_manager.upgrade_themes = MagicMock() Settings().setValue('themes/global theme', 'my_theme') # WHEN: the initialisation is run - with patch('openlp.core.ui.thememanager.ThemeProgressForm'): - self.theme_manager.bootstrap_initialise() + self.theme_manager.bootstrap_initialise() # THEN: self.theme_manager.setup_ui.assert_called_once_with(self.theme_manager) assert self.theme_manager.global_theme == 'my_theme' self.theme_manager.build_theme_path.assert_called_once_with() - self.theme_manager.load_first_time_themes.assert_called_once_with() self.theme_manager.upgrade_themes.assert_called_once_with() @patch('openlp.core.ui.thememanager.create_paths') @@ -117,7 +114,8 @@ class TestThemeManager(TestCase, TestMixin): self.theme_manager.theme_path = MagicMock() # WHEN: - self.theme_manager.bootstrap_post_set_up() + with patch('openlp.core.ui.thememanager.ThemeProgressForm'): + self.theme_manager.bootstrap_post_set_up() # THEN: assert 1 == self.theme_manager.load_themes.call_count, "load_themes should have been called once" diff --git a/tests/openlp_core/ui/test_themeprogressform.py b/tests/openlp_core/ui/test_themeprogressform.py new file mode 100644 index 000000000..dfb91f496 --- /dev/null +++ b/tests/openlp_core/ui/test_themeprogressform.py @@ -0,0 +1,142 @@ +# -*- coding: utf-8 -*- + +########################################################################## +# OpenLP - Open Source Lyrics Projection # +# ---------------------------------------------------------------------- # +# Copyright (c) 2008-2019 OpenLP Developers # +# ---------------------------------------------------------------------- # +# 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 . # +########################################################################## +""" +Package to test the openlp.core.ui.themeform package. +""" +# from pathlib import Path +from unittest import TestCase +from unittest.mock import MagicMock, patch + +from openlp.core.ui.themeprogressform import ThemeProgressForm +from tests.helpers.testmixin import TestMixin + + +class TestThemeProgressForm(TestCase, TestMixin): + """ + Test the functions in the ThemeProgressForm class + """ + def setUp(self): + self.setup_application() + + def tearDown(self): + del self.app + + def _get_theme_progress_form(self): + """Common code used to create the ThemeProgressForm object""" + with patch('openlp.core.ui.themeprogressdialog.ThemePreviewRenderer'), \ + patch('openlp.core.ui.themeprogressdialog.UiThemeProgressDialog.setup_ui'): + form = ThemeProgressForm() + return form + + def test_init(self): + """Test that the ThemeProgressForm is created without problems""" + # GIVEN: ThemeProgressForm class + # WHEN: An object is instatiated + # THEN: There is no problem + self._get_theme_progress_form() + + @patch('openlp.core.ui.themeprogressform.ScreenList') + @patch('openlp.core.ui.themeprogressform.QtWidgets.QDialog.show') + def test_show(self, mocked_show, MockScreenList): + """Test that the ThemeProgressForm is created without problems""" + # GIVEN: ThemeProgressForm object + form = self._get_theme_progress_form() + mocked_screen_list = MagicMock() + mocked_screen_list.current.display_geometry.width.return_value = 1920 + mocked_screen_list.current.display_geometry.height.return_value = 1080 + MockScreenList.return_value = mocked_screen_list + form.progress_bar = MagicMock() + form.theme_preview_layout = MagicMock() + + # WHEN: The show() method is called + form.show() + + # THEN: The correct display ratio is calculated and the form is shown + expected_ratio = 16 / 9 + form.progress_bar.setMinimum.assert_called_once_with(0) + form.progress_bar.setMaximum.assert_called_once_with(0) + form.progress_bar.setValue.assert_called_once_with(0) + assert form.ratio == expected_ratio + assert form.theme_preview_layout.aspect_ratio == expected_ratio + mocked_show.assert_called_once() + + @patch('openlp.core.ui.themeprogressform.ScreenList') + @patch('openlp.core.ui.themeprogressform.QtWidgets.QDialog.show') + def test_show_divide_by_zero(self, mocked_show, MockScreenList): + """Test that the ThemeProgressForm is created without problems even if there's a divide by zero exception""" + # GIVEN: ThemeProgressForm object + form = self._get_theme_progress_form() + mocked_screen_list = MagicMock() + mocked_screen_list.current.display_geometry.width.return_value = 1920 + mocked_screen_list.current.display_geometry.height.return_value = 0 + MockScreenList.return_value = mocked_screen_list + form.progress_bar = MagicMock() + form.theme_preview_layout = MagicMock() + + # WHEN: The show() method is called + form.show() + + # THEN: The correct display ratio is calculated and the form is shown + expected_ratio = 16 / 9 + form.progress_bar.setMinimum.assert_called_once_with(0) + form.progress_bar.setMaximum.assert_called_once_with(0) + form.progress_bar.setValue.assert_called_once_with(0) + assert form.ratio == expected_ratio + assert form.theme_preview_layout.aspect_ratio == expected_ratio + mocked_show.assert_called_once() + + def test_get_preview(self): + """Test that the get_preview() method returns a preview image""" + # GIVEN: ThemeProgressForm object + test_theme_name = 'Test Theme' + test_theme_data = {'name': test_theme_name} + form = self._get_theme_progress_form() + form.progress_bar = MagicMock(**{'value.return_value': 0}) + form.label = MagicMock() + form.theme_display = MagicMock(**{'width.return_value': 192, 'generate_preview.return_value': 'preview'}) + form.renderer.width = MagicMock(return_value=1920) + + # WHEN: get_preview() is called + preview = form.get_preview(test_theme_name, test_theme_data) + + # THEN: All the correct methods should be called and the correct results should be returned + form.label.setText.assert_called_once_with(test_theme_name) + form.progress_bar.value.assert_called_once() + form.progress_bar.setValue.assert_called_once_with(1) + form.theme_display.width.assert_called_once() + form.renderer.width.assert_called_once() + form.theme_display.set_scale.assert_called_once_with(0.1) + form.theme_display.generate_preview.assert_called_once_with(test_theme_data, generate_screenshot=True) + assert preview == 'preview' + + def test_theme_list(self): + # GIVEN: ThemeProgressForm object and theme list + test_theme_list = ['Theme 1', 'Theme 2'] + form = self._get_theme_progress_form() + form.progress_bar = MagicMock() + + # WHEN: theme_list is set and get'ed + form.theme_list = test_theme_list + theme_list = form.theme_list + + # THEN: The theme list should be correct + form.progress_bar.setMaximum.assert_called_once_with(2) + assert theme_list == test_theme_list