From b606aaeb7f5ac0218e58074ad77f11ddaf0ebe8c Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Sun, 15 Nov 2015 08:55:13 +0000 Subject: [PATCH] fix crash for remote alerts and add tests --- openlp/plugins/alerts/lib/alertsmanager.py | 6 +- .../openlp_plugins/alerts/__init__.py | 21 +++++ .../openlp_plugins/alerts/test_manager.py | 84 +++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 tests/functional/openlp_plugins/alerts/__init__.py create mode 100644 tests/functional/openlp_plugins/alerts/test_manager.py diff --git a/openlp/plugins/alerts/lib/alertsmanager.py b/openlp/plugins/alerts/lib/alertsmanager.py index 8d691c2bb..7eb3f07c0 100644 --- a/openlp/plugins/alerts/lib/alertsmanager.py +++ b/openlp/plugins/alerts/lib/alertsmanager.py @@ -48,7 +48,11 @@ class AlertsManager(OpenLPMixin, RegistryMixin, QtCore.QObject, RegistryProperti :param message: The message text to be displayed """ if message: - self.display_alert(message[0]) + text = message[0] + # remove line breaks as these crash javascript code on display + while '\n' in text: + text = text.replace('\n', ' ') + self.display_alert(text) def display_alert(self, text=''): """ diff --git a/tests/functional/openlp_plugins/alerts/__init__.py b/tests/functional/openlp_plugins/alerts/__init__.py new file mode 100644 index 000000000..9552a683f --- /dev/null +++ b/tests/functional/openlp_plugins/alerts/__init__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2015 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; version 2 of the License. # +# # +# 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, write to the Free Software Foundation, Inc., 59 # +# Temple Place, Suite 330, Boston, MA 02111-1307 USA # +############################################################################### \ No newline at end of file diff --git a/tests/functional/openlp_plugins/alerts/test_manager.py b/tests/functional/openlp_plugins/alerts/test_manager.py new file mode 100644 index 000000000..e81ec8cb2 --- /dev/null +++ b/tests/functional/openlp_plugins/alerts/test_manager.py @@ -0,0 +1,84 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2015 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; version 2 of the License. # +# # +# 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, write to the Free Software Foundation, Inc., 59 # +# Temple Place, Suite 330, Boston, MA 02111-1307 USA # +############################################################################### +""" +This module contains tests for the CSV Bible importer. +""" + +import os +import json +from unittest import TestCase + +from tests.functional import MagicMock, patch +from openlp.core.common.registry import Registry +from openlp.plugins.alerts.lib.alertsmanager import AlertsManager + + +class TestAlertManager(TestCase): + + def setUp(self): + """ + Create the UI + """ + Registry.create() + + def remove_message_text_test(self): + """ + Test that Alerts are not triggered with empty strings + """ + # GIVEN: A valid Alert Manager + alert_manager = AlertsManager(None) + alert_manager.display_alert = MagicMock() + + # WHEN: Called with an empty string + alert_manager.alert_text('') + + # THEN: the display should not have been triggered + self.assertFalse(alert_manager.display_alert.called, 'The Alert should not have been called') + + def trigger_message_text_test(self): + """ + Test that Alerts are triggered with a text string + """ + # GIVEN: A valid Alert Manager + alert_manager = AlertsManager(None) + alert_manager.display_alert = MagicMock() + + # WHEN: Called with an empty string + alert_manager.alert_text(['This is a string']) + + # THEN: the display should have been triggered + self.assertTrue(alert_manager.display_alert.called, 'The Alert should have been called') + + def line_break_message_text_test(self): + """ + Test that Alerts are triggered with a text string but line breaks are removed + """ + # GIVEN: A valid Alert Manager + alert_manager = AlertsManager(None) + alert_manager.display_alert = MagicMock() + + # WHEN: Called with an empty string + alert_manager.alert_text(['This is \n a string']) + + # THEN: the display should have been triggered + self.assertTrue(alert_manager.display_alert.called, 'The Alert should have been called') + alert_manager.display_alert.assert_called_once_with('This is a string')