~gtalent/openlp/openlp

« back to all changes in this revision

Viewing changes to tests/functional/openlp_core_ui_lib/test_listwidgetwithdnd.py

  • Committer: Gary Talent
  • Date: 2017-03-29 21:40:54 UTC
  • mfrom: (2695.1.35 openlp)
  • Revision ID: gtalent2@gmail.com-20170329214054-62r3rqvv3c0raagb
Update to trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
 
3
 
 
4
###############################################################################
 
5
# OpenLP - Open Source Lyrics Projection                                      #
 
6
# --------------------------------------------------------------------------- #
 
7
# Copyright (c) 2008-2017 OpenLP Developers                                   #
 
8
# --------------------------------------------------------------------------- #
 
9
# This program is    free software; you can redistribute it and/or modify it     #
 
10
# under the terms of the GNU General Public License as published by the Free  #
 
11
# Software Foundation; version 2 of the License.                              #
 
12
#                                                                             #
 
13
# This program is distributed in the hope that it will be useful, but WITHOUT #
 
14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
 
15
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    #
 
16
# more details.                                                               #
 
17
#                                                                             #
 
18
# You should have received a copy of the GNU General Public License along     #
 
19
# with this program; if not, write to the Free Software Foundation, Inc., 59  #
 
20
# Temple Place, Suite 330, Boston, MA 02111-1307 USA                          #
 
21
###############################################################################
 
22
"""
 
23
This module contains tests for the openlp.core.lib.listwidgetwithdnd module
 
24
"""
 
25
from unittest import TestCase
 
26
 
 
27
from openlp.core.common.uistrings import UiStrings
 
28
from openlp.core.ui.lib.listwidgetwithdnd import ListWidgetWithDnD
 
29
from unittest.mock import MagicMock, patch
 
30
 
 
31
 
 
32
class TestListWidgetWithDnD(TestCase):
 
33
    """
 
34
    Test the :class:`~openlp.core.lib.listwidgetwithdnd.ListWidgetWithDnD` class
 
35
    """
 
36
    def test_clear_locked(self):
 
37
        """
 
38
        Test the clear method the list is 'locked'
 
39
        """
 
40
        with patch('openlp.core.ui.lib.listwidgetwithdnd.QtWidgets.QListWidget.clear') as mocked_clear_super_method:
 
41
            # GIVEN: An instance of ListWidgetWithDnD
 
42
            widget = ListWidgetWithDnD()
 
43
 
 
44
            # WHEN: The list is 'locked' and clear has been called
 
45
            widget.locked = True
 
46
            widget.clear()
 
47
 
 
48
            # THEN: The super method should not have been called (i.e. The list not cleared)
 
49
            self.assertFalse(mocked_clear_super_method.called)
 
50
 
 
51
    def test_clear_overide_locked(self):
 
52
        """
 
53
        Test the clear method the list is 'locked', but clear is called with 'override_lock' set to True
 
54
        """
 
55
        with patch('openlp.core.ui.lib.listwidgetwithdnd.QtWidgets.QListWidget.clear') as mocked_clear_super_method:
 
56
            # GIVEN: An instance of ListWidgetWithDnD
 
57
            widget = ListWidgetWithDnD()
 
58
 
 
59
            # WHEN: The list is 'locked' and clear has been called with override_lock se to True
 
60
            widget.locked = True
 
61
            widget.clear(override_lock=True)
 
62
 
 
63
            # THEN: The super method should have been called (i.e. The list is cleared regardless whether it is locked
 
64
            #       or not)
 
65
            mocked_clear_super_method.assert_called_once_with()
 
66
 
 
67
    def test_clear(self):
 
68
        """
 
69
        Test the clear method when called without any arguments.
 
70
        """
 
71
        # GIVEN: An instance of ListWidgetWithDnD
 
72
        widget = ListWidgetWithDnD()
 
73
 
 
74
        # WHEN: Calling clear with out any arguments
 
75
        widget.clear()
 
76
 
 
77
        # THEN: The results text should be the standard 'no results' text.
 
78
        self.assertEqual(widget.no_results_text, UiStrings().NoResults)
 
79
 
 
80
    def test_clear_search_while_typing(self):
 
81
        """
 
82
        Test the clear method when called with the search_while_typing argument set to True
 
83
        """
 
84
        # GIVEN: An instance of ListWidgetWithDnD
 
85
        widget = ListWidgetWithDnD()
 
86
 
 
87
        # WHEN: Calling clear with search_while_typing set to True
 
88
        widget.clear(search_while_typing=True)
 
89
 
 
90
        # THEN: The results text should be the 'short results' text.
 
91
        self.assertEqual(widget.no_results_text, UiStrings().ShortResults)
 
92
 
 
93
    def test_paint_event(self):
 
94
        """
 
95
        Test the paintEvent method when the list is not empty
 
96
        """
 
97
        # GIVEN: An instance of ListWidgetWithDnD with a mocked out count methode which returns 1
 
98
        #       (i.e the list has an item)
 
99
        widget = ListWidgetWithDnD()
 
100
        with patch('openlp.core.ui.lib.listwidgetwithdnd.QtWidgets.QListWidget.paintEvent') as mocked_paint_event, \
 
101
                patch.object(widget, 'count', return_value=1), \
 
102
                patch.object(widget, 'viewport') as mocked_viewport:
 
103
            mocked_event = MagicMock()
 
104
 
 
105
            # WHEN: Calling paintEvent
 
106
            widget.paintEvent(mocked_event)
 
107
 
 
108
            # THEN: The overridden paintEvnet should have been called
 
109
            mocked_paint_event.assert_called_once_with(mocked_event)
 
110
            self.assertFalse(mocked_viewport.called)
 
111
 
 
112
    def test_paint_event_no_items(self):
 
113
        """
 
114
        Test the paintEvent method when the list is empty
 
115
        """
 
116
        # GIVEN: An instance of ListWidgetWithDnD with a mocked out count methode which returns 0
 
117
        #       (i.e the list is empty)
 
118
        widget = ListWidgetWithDnD()
 
119
        mocked_painter_instance = MagicMock()
 
120
        mocked_qrect = MagicMock()
 
121
        with patch('openlp.core.ui.lib.listwidgetwithdnd.QtWidgets.QListWidget.paintEvent') as mocked_paint_event, \
 
122
                patch.object(widget, 'count', return_value=0), \
 
123
                patch.object(widget, 'viewport'), \
 
124
                patch('openlp.core.ui.lib.listwidgetwithdnd.QtGui.QPainter',
 
125
                      return_value=mocked_painter_instance) as mocked_qpainter, \
 
126
                patch('openlp.core.ui.lib.listwidgetwithdnd.QtCore.QRect', return_value=mocked_qrect):
 
127
            mocked_event = MagicMock()
 
128
 
 
129
            # WHEN: Calling paintEvent
 
130
            widget.paintEvent(mocked_event)
 
131
 
 
132
            # THEN: The overridden paintEvnet should have been called, and some text should be drawn.
 
133
            mocked_paint_event.assert_called_once_with(mocked_event)
 
134
            mocked_qpainter.assert_called_once_with(widget.viewport())
 
135
            mocked_painter_instance.drawText.assert_called_once_with(mocked_qrect, 4100, 'No Search Results')