1
# -*- coding: utf-8 -*-
2
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
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. #
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 #
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
###############################################################################
23
This module contains tests for the openlp.core.lib.listwidgetwithdnd module
25
from unittest import TestCase
27
from openlp.core.common.uistrings import UiStrings
28
from openlp.core.ui.lib.listwidgetwithdnd import ListWidgetWithDnD
29
from unittest.mock import MagicMock, patch
32
class TestListWidgetWithDnD(TestCase):
34
Test the :class:`~openlp.core.lib.listwidgetwithdnd.ListWidgetWithDnD` class
36
def test_clear_locked(self):
38
Test the clear method the list is 'locked'
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()
44
# WHEN: The list is 'locked' and clear has been called
48
# THEN: The super method should not have been called (i.e. The list not cleared)
49
self.assertFalse(mocked_clear_super_method.called)
51
def test_clear_overide_locked(self):
53
Test the clear method the list is 'locked', but clear is called with 'override_lock' set to True
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()
59
# WHEN: The list is 'locked' and clear has been called with override_lock se to True
61
widget.clear(override_lock=True)
63
# THEN: The super method should have been called (i.e. The list is cleared regardless whether it is locked
65
mocked_clear_super_method.assert_called_once_with()
69
Test the clear method when called without any arguments.
71
# GIVEN: An instance of ListWidgetWithDnD
72
widget = ListWidgetWithDnD()
74
# WHEN: Calling clear with out any arguments
77
# THEN: The results text should be the standard 'no results' text.
78
self.assertEqual(widget.no_results_text, UiStrings().NoResults)
80
def test_clear_search_while_typing(self):
82
Test the clear method when called with the search_while_typing argument set to True
84
# GIVEN: An instance of ListWidgetWithDnD
85
widget = ListWidgetWithDnD()
87
# WHEN: Calling clear with search_while_typing set to True
88
widget.clear(search_while_typing=True)
90
# THEN: The results text should be the 'short results' text.
91
self.assertEqual(widget.no_results_text, UiStrings().ShortResults)
93
def test_paint_event(self):
95
Test the paintEvent method when the list is not empty
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()
105
# WHEN: Calling paintEvent
106
widget.paintEvent(mocked_event)
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)
112
def test_paint_event_no_items(self):
114
Test the paintEvent method when the list is empty
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()
129
# WHEN: Calling paintEvent
130
widget.paintEvent(mocked_event)
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')