1
# -*- coding: utf-8 -*-
3
# Copyright 2012 Canonical Ltd.
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License version 3, as published
7
# by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License along
15
# with this program. If not, see <http://www.gnu.org/licenses/>.
17
# In addition, as a special exception, the copyright holders give
18
# permission to link the code of portions of this program with the
19
# OpenSSL library under certain conditions as described in each
20
# individual source file, and distribute linked combinations
22
# You must obey the GNU General Public License in all respects
23
# for all of the code used other than OpenSSL. If you modify
24
# file(s) with this exception, you may extend this exception to your
25
# version of the file(s), but you are not obligated to do so. If you
26
# do not wish to do so, delete this exception statement from your
27
# version. If you delete this exception statement from all source
28
# files in the program, then also delete it here.
31
from PyQt4.QtGui import QStyle
32
from twisted.internet import defer
33
from twisted.trial.unittest import TestCase
35
from ubuntu_sso.qt.arrow import QArrow
37
# pylint: disable=W0212,C0103
40
class FakeStyle(object):
41
"""Fake a Qt style."""
44
"""Create instance."""
47
def __call__(self, *args, **kwargs):
48
"""Instance callable."""
51
def drawPrimitive(self, primitive, opt, painter, widget):
52
"""Fake a draw primitive."""
53
self.called.append(('drawPrimitive', primitive, opt, painter, widget))
56
class QArrowTestCase(TestCase):
57
"""Test the QArrow class."""
59
@defer.inlineCallbacks
62
yield super(QArrowTestCase, self).setUp()
63
self.valid_dirs = (QArrow.UP, QArrow.DOWN,
64
QArrow.LEFT, QArrow.RIGHT)
65
self.style = FakeStyle()
67
def test_correct_init(self):
68
"""Test the init using the correct direction."""
69
for direction in self.valid_dirs:
70
arrow = QArrow(direction)
71
self.assertEqual(direction, arrow.direction)
73
def test_wrong_init(self):
74
"""Test the init using the wrong direction."""
75
self.assertRaises(ValueError, QArrow, 'test')
77
def test_set_direction_correct(self):
78
"""Test the set direction with the correct value."""
79
arrow = QArrow(QArrow.RIGHT)
80
for direction in self.valid_dirs:
81
arrow.direction = direction
82
self.assertEqual(direction, arrow.direction)
84
def test_set_direction_wrong(self):
85
"""Test set direction wrong."""
86
arrow = QArrow(QArrow.RIGHT)
87
self.assertRaises(ValueError, setattr, arrow, 'direction', 'wrong')
89
def test_paint_event(self):
90
"""Test the paint event."""
91
directions_map = {QArrow.UP: QStyle.PE_IndicatorArrowUp,
92
QArrow.DOWN: QStyle.PE_IndicatorArrowDown,
93
QArrow.LEFT: QStyle.PE_IndicatorArrowLeft,
94
QArrow.RIGHT: QStyle.PE_IndicatorArrowRight}
96
arrow = QArrow(QArrow.RIGHT)
97
self.patch(arrow, 'style', self.style)
99
for key, value in directions_map.iteritems():
100
self.style.called = []
101
arrow.direction = key
102
arrow.paintEvent(None)
103
self.assertIn(value, self.style.called[0])