openlp/tests/interfaces/openlp_plugins/custom/forms/test_customform.py

145 lines
6.0 KiB
Python
Raw Normal View History

2014-03-14 22:08:44 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
2019-04-13 13:00:22 +00:00
##########################################################################
# 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 <https://www.gnu.org/licenses/>. #
##########################################################################
2013-03-28 20:03:58 +00:00
"""
2013-03-30 09:01:21 +00:00
Module to test the EditCustomForm.
2013-03-28 20:03:58 +00:00
"""
from unittest import TestCase
from unittest.mock import MagicMock, patch
2013-03-28 20:03:58 +00:00
2018-10-02 04:39:42 +00:00
from PyQt5 import QtCore, QtTest, QtWidgets
2013-03-28 20:03:58 +00:00
2017-10-07 07:05:07 +00:00
from openlp.core.common.registry import Registry
2013-03-28 20:03:58 +00:00
from openlp.plugins.custom.forms.editcustomform import EditCustomForm
2014-03-14 22:08:44 +00:00
from tests.helpers.testmixin import TestMixin
2013-03-28 20:03:58 +00:00
2014-03-14 22:08:44 +00:00
class TestEditCustomForm(TestCase, TestMixin):
2013-03-28 20:03:58 +00:00
"""
Test the EditCustomForm.
"""
def setUp(self):
"""
Create the UI
"""
Registry.create()
self.setup_application()
2015-11-07 00:49:40 +00:00
self.main_window = QtWidgets.QMainWindow()
2013-08-31 18:17:38 +00:00
Registry().register('main_window', self.main_window)
2013-03-28 20:03:58 +00:00
media_item = MagicMock()
manager = MagicMock()
self.form = EditCustomForm(media_item, self.main_window, manager)
def tearDown(self):
"""
Delete all the C++ objects at the end so that we don't have a segfault
"""
del self.form
del self.main_window
2013-03-28 20:03:58 +00:00
2016-05-31 21:40:13 +00:00
def test_load_themes(self):
2013-03-30 09:46:32 +00:00
"""
Test the load_themes() method.
"""
2013-04-08 18:30:11 +00:00
# GIVEN: A theme list.
2013-08-31 18:17:38 +00:00
theme_list = ['First Theme', 'Second Theme']
2013-04-08 18:30:11 +00:00
# WHEN: Show the dialog and add pass a theme list.
self.form.load_themes(theme_list)
2013-03-30 09:46:32 +00:00
2013-04-08 18:30:11 +00:00
# THEN: There should be three items in the combo box.
2013-08-31 18:17:38 +00:00
assert self.form.theme_combo_box.count() == 3, 'There should be three items (themes) in the combo box.'
2013-03-30 09:46:32 +00:00
2016-05-31 21:40:13 +00:00
def test_load_custom(self):
2013-03-28 20:03:58 +00:00
"""
2013-03-28 22:08:14 +00:00
Test the load_custom() method.
2013-03-28 20:03:58 +00:00
"""
2013-04-08 18:30:11 +00:00
# WHEN: Create a new custom item.
self.form.load_custom(0)
2013-03-28 20:03:58 +00:00
2013-04-08 18:30:11 +00:00
# THEN: The line edits should not contain any text.
2017-12-23 09:09:45 +00:00
assert self.form.title_edit.text() == '', 'The title edit should be empty'
assert self.form.credit_edit.text() == '', 'The credit edit should be empty'
2013-03-28 20:03:58 +00:00
2016-05-31 21:40:13 +00:00
def test_on_add_button_clicked(self):
2013-03-28 22:08:14 +00:00
"""
Test the on_add_button_clicked_test method / add_button button.
"""
2015-11-07 00:49:40 +00:00
# GIVEN: A mocked QDialog.exec() method
2018-10-27 01:40:20 +00:00
with patch('PyQt5.QtWidgets.QDialog.exec'):
2013-04-08 18:30:11 +00:00
# WHEN: Add a new slide.
2013-03-28 22:08:14 +00:00
QtTest.QTest.mouseClick(self.form.add_button, QtCore.Qt.LeftButton)
2013-04-08 18:30:11 +00:00
2013-03-30 08:46:34 +00:00
# THEN: One slide should be added.
2013-08-31 18:17:38 +00:00
assert self.form.slide_list_view.count() == 1, 'There should be one slide added.'
2013-04-17 18:33:44 +00:00
2016-05-31 21:40:13 +00:00
def test_validate_not_valid_part1(self):
2013-04-17 18:33:44 +00:00
"""
Test the _validate() method.
"""
# GIVEN: Mocked methods.
2013-08-31 18:17:38 +00:00
with patch('openlp.plugins.custom.forms.editcustomform.critical_error_message_box') as \
2013-04-17 18:33:44 +00:00
mocked_critical_error_message_box:
2013-08-31 18:17:38 +00:00
self.form.title_edit.displayText = MagicMock(return_value='')
2013-04-17 18:33:44 +00:00
mocked_setFocus = MagicMock()
self.form.title_edit.setFocus = mocked_setFocus
# WHEN: Call the method.
result = self.form._validate()
# THEN: The validate method should have returned False.
2013-08-31 18:17:38 +00:00
assert not result, 'The _validate() method should have retured False'
2013-04-17 18:33:44 +00:00
mocked_setFocus.assert_called_with()
2013-08-31 18:17:38 +00:00
mocked_critical_error_message_box.assert_called_with(message='You need to type in a title.')
2013-04-17 18:33:44 +00:00
2016-05-31 21:40:13 +00:00
def test_validate_not_valid_part2(self):
2013-04-17 18:33:44 +00:00
"""
Test the _validate() method.
"""
# GIVEN: Mocked methods.
2013-08-31 18:17:38 +00:00
with patch('openlp.plugins.custom.forms.editcustomform.critical_error_message_box') as \
2013-04-17 18:33:44 +00:00
mocked_critical_error_message_box:
2013-08-31 18:17:38 +00:00
self.form.title_edit.displayText = MagicMock(return_value='something')
2013-07-17 14:19:21 +00:00
self.form.slide_list_view.count = MagicMock(return_value=0)
2013-04-17 18:33:44 +00:00
# WHEN: Call the method.
result = self.form._validate()
# THEN: The validate method should have returned False.
2013-08-31 18:17:38 +00:00
assert not result, 'The _validate() method should have retured False'
mocked_critical_error_message_box.assert_called_with(message='You need to add at least one slide.')
2016-05-31 21:40:13 +00:00
def test_update_slide_list(self):
"""
Test the update_slide_list() method
"""
# GIVEN: Mocked slide_list_view with a slide with 3 lines
self.form.slide_list_view = MagicMock()
self.form.slide_list_view.count.return_value = 1
self.form.slide_list_view.currentRow.return_value = 0
self.form.slide_list_view.item.return_value = MagicMock(return_value='1st Slide\n2nd Slide\n3rd Slide')
# WHEN: updating the slide by splitting the lines into slides
self.form.update_slide_list(['1st Slide', '2nd Slide', '3rd Slide'])
# THEN: The slides should be created in correct order
self.form.slide_list_view.addItems.assert_called_with(['1st Slide', '2nd Slide', '3rd Slide'])