From 7c4d9a5ffde2cd870869f5aeb8fb84b3cff77a51 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 3 Dec 2019 17:30:35 +0000 Subject: [PATCH] Time out after 10 seconds in wait till loaded fn also fixes comment, update_preview_images() does not handle the timeout case but in reality it should never happen. (just in the case it does, it won't stall the whole application) --- openlp/core/display/render.py | 18 +++++++++++ openlp/core/display/window.py | 4 ++- openlp/core/ui/thememanager.py | 1 + .../openlp_core/display/test_window.py | 30 +++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/openlp/core/display/render.py b/openlp/core/display/render.py index 5552a620c..f71aa2693 100644 --- a/openlp/core/display/render.py +++ b/openlp/core/display/render.py @@ -486,6 +486,24 @@ class ThemePreviewRenderer(LogMixin, DisplayWindow): footer_html = 'Dummy footer text' return footer_html + def wait_till_loaded(self): + """ + Wait until web engine page loaded + :return boolean: True on success, False on timeout + """ + # Timeout in 10 seconds + end_time = time.time() + 10 + app = Registry().get('application') + success = True + while not self._is_initialised: + if time.time() > end_time: + log.error('Timed out waiting for web engine page to load') + success = False + break + time.sleep(0.1) + app.process_events() + return success + def _wait_and_process(self, delay): """ Wait while allowing things to process diff --git a/openlp/core/display/window.py b/openlp/core/display/window.py index 52c1a8fc3..ac15c1ade 100644 --- a/openlp/core/display/window.py +++ b/openlp/core/display/window.py @@ -402,7 +402,9 @@ class DisplayWindow(QtWidgets.QWidget): Set the HTML scale """ self.scale = scale - self.run_javascript('Display.setScale({scale});'.format(scale=scale * 100)) + # Only scale if initialised (scale run again once initialised) + if self._is_initialised: + self.run_javascript('Display.setScale({scale});'.format(scale=scale * 100)) def alert(self, text, settings): """ diff --git a/openlp/core/ui/thememanager.py b/openlp/core/ui/thememanager.py index 6710ff507..22a158fb4 100644 --- a/openlp/core/ui/thememanager.py +++ b/openlp/core/ui/thememanager.py @@ -722,6 +722,7 @@ class ThemeManager(QtWidgets.QWidget, RegistryBase, Ui_ThemeManager, LogMixin, R theme_name_list = theme_name_list or self.get_theme_names() self.progress_form.theme_list = theme_name_list self.progress_form.show() + self.progress_form.theme_display.wait_till_loaded() for theme_name in theme_name_list: theme_data = self._get_theme_data(theme_name) preview_pixmap = self.progress_form.get_preview(theme_name, theme_data) diff --git a/tests/functional/openlp_core/display/test_window.py b/tests/functional/openlp_core/display/test_window.py index df1b8aded..180195c2c 100644 --- a/tests/functional/openlp_core/display/test_window.py +++ b/tests/functional/openlp_core/display/test_window.py @@ -74,3 +74,33 @@ class TestDisplayWindow(TestCase, TestMixin): # THEN: The x11 override flag should not be set x11_bit = display_window.windowFlags() & QtCore.Qt.X11BypassWindowManagerHint assert x11_bit != QtCore.Qt.X11BypassWindowManagerHint + + def test_set_scale_not_initialised(self, MockSettings, mocked_webengine, mocked_addWidget): + """ + Test that the scale js is not run if the page is not initialised + """ + # GIVEN: A display window not yet initialised + display_window = DisplayWindow() + display_window._is_initialised = False + display_window.run_javascript = MagicMock() + + # WHEN: set scale is run + display_window.set_scale(0.5) + + # THEN: javascript should not be run + display_window.run_javascript.assert_not_called() + + def test_set_scale_initialised(self, MockSettings, mocked_webengine, mocked_addWidget): + """ + Test that the scale js is not run if the page is not initialised + """ + # GIVEN: A display window not yet initialised + display_window = DisplayWindow() + display_window._is_initialised = True + display_window.run_javascript = MagicMock() + + # WHEN: set scale is run + display_window.set_scale(0.5) + + # THEN: javascript should not be run + display_window.run_javascript.assert_called_once_with('Display.setScale(50.0);')