File: test_customform.py

package info (click to toggle)
openlp 3.1.7%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 50,372 kB
  • sloc: python: 79,350; javascript: 5,572; xml: 1,019; sh: 65; makefile: 29
file content (151 lines) | stat: -rw-r--r-- 5,804 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# -*- coding: utf-8 -*-

##########################################################################
# OpenLP - Open Source Lyrics Projection                                 #
# ---------------------------------------------------------------------- #
# Copyright (c) 2008-2024 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/>. #
##########################################################################
"""
Module to test the EditCustomForm.
"""
import pytest
from unittest.mock import MagicMock, patch

from PyQt5 import QtCore, QtTest, QtWidgets

from openlp.plugins.custom.forms.editcustomform import EditCustomForm


@pytest.fixture()
def form(settings):
    main_window = QtWidgets.QMainWindow()
    media_item = MagicMock()
    manager = MagicMock()
    frm = EditCustomForm(media_item, main_window, manager)
    yield frm
    del frm
    del main_window


def test_load_themes(form):
    """
    Test the load_themes() method.
    """
    # GIVEN: A theme list.
    theme_list = ['First Theme', 'Second Theme']

    # WHEN: Show the dialog and add pass a theme list.
    form.load_themes(theme_list)

    # THEN: There should be three items in the combo box.
    assert form.theme_combo_box.count() == 3, 'There should be three items (themes) in the combo box.'


def test_load_custom(form):
    """
    Test the load_custom() method.
    """
    # WHEN: Create a new custom item.
    form.load_custom(0)

    # THEN: The line edits should not contain any text.
    assert form.title_edit.text() == '', 'The title edit should be empty'
    assert form.credit_edit.text() == '', 'The credit edit should be empty'


def test_on_add_button_clicked(form):
    """
    Test the on_add_button_clicked_test method / add_button button.
    """
    # GIVEN: A mocked QDialog.exec() method
    with patch('PyQt5.QtWidgets.QDialog.exec'):
        # WHEN: Add a new slide.
        QtTest.QTest.mouseClick(form.add_button, QtCore.Qt.LeftButton)

        # THEN: One slide should be added.
        assert form.slide_list_view.count() == 1, 'There should be one slide added.'


def test_validate_not_valid_part1(form):
    """
    Test the _validate() method.
    """
    # GIVEN: Mocked methods.
    with patch('openlp.plugins.custom.forms.editcustomform.critical_error_message_box') as \
            mocked_critical_error_message_box:
        form.title_edit.displayText = MagicMock(return_value='')
        mocked_setFocus = MagicMock()
        form.title_edit.setFocus = mocked_setFocus

        # WHEN: Call the method.
        result = form._validate()

        # THEN: The validate method should have returned False.
        assert not result, 'The _validate() method should have retured False'
        mocked_setFocus.assert_called_with()
        mocked_critical_error_message_box.assert_called_with(message='You need to type in a title.')


def test_validate_not_valid_part2(form):
    """
    Test the _validate() method.
    """
    # GIVEN: Mocked methods.
    with patch('openlp.plugins.custom.forms.editcustomform.critical_error_message_box') as \
            mocked_critical_error_message_box:
        form.title_edit.displayText = MagicMock(return_value='something')
        form.slide_list_view.count = MagicMock(return_value=0)

        # WHEN: Call the method.
        result = form._validate()

        # THEN: The validate method should have returned False.
        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.')


def test_update_slide_list(form):
    """
    Test the update_slide_list() method
    """
    # GIVEN: Mocked slide_list_view with a slide with 3 lines
    form.slide_list_view = MagicMock()
    form.slide_list_view.count.return_value = 1
    form.slide_list_view.currentRow.return_value = 0
    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
    form.update_slide_list(['1st Slide', '2nd Slide', '3rd Slide'])

    # THEN: The slides should be created in correct order
    form.slide_list_view.addItems.assert_called_with(['1st Slide', '2nd Slide', '3rd Slide'])


@patch.object(EditCustomForm, 'provide_help')
def test_help(mocked_help, settings):
    """
    Test the help button
    """
    # GIVEN: An edit custom slide form and a patched help function
    main_window = QtWidgets.QMainWindow()
    custom_form = EditCustomForm(MagicMock(), main_window, MagicMock())

    # WHEN: The Help button is clicked
    QtTest.QTest.mouseClick(custom_form.button_box.button(QtWidgets.QDialogButtonBox.StandardButton.Help),
                            QtCore.Qt.LeftButton)

    # THEN: The Help function should be called
    mocked_help.assert_called_once()