~mandel/ubuntu-sso-client/pinned-certs

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# -*- coding: utf-8 -*-
#
# Copyright 2012 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, 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 <http://www.gnu.org/licenses/>.
"""Tests for the expander widget."""

from twisted.internet import defer
from twisted.trial.unittest import TestCase

from ubuntu_sso.qt.arrow import QArrow
from ubuntu_sso.qt.expander import QExpanderLabel, QExpander

# pylint: disable=C0103, W0201, W0212


class FakeArrow(object):
    """Fake arrow widget."""

    def __init__(self):
        self.direction = QArrow.UP
        self.called = []

    def repaint(self):
        """Fake repaint."""
        self.called.append(('repaint',))


class FakeLabel(object):
    """Fake a label."""

    def __init__(self, content):
        """Create a new instance."""
        self.content = content
        self.called = []

    def text(self):
        """Fake text."""
        self.called.append(('text', ))
        return self.content

    def setText(self, content):
        """Fake set text."""
        self.called.append(('setText', content))
        self.content = content

    def setVisible(self, visible):
        """Fake setVisible."""
        self.called.append(('setVisible', visible))


class FakeLayout(object):
    """A fake Wt layout."""

    def __init__(self):
        """Create an instance."""
        self.called = []

    def insertWidget(self, index, widget):
        """Insert a new widget."""
        self.called.append(('insertWidget', index, widget))

    def removeWidget(self, widget):
        """Remove a given widget."""
        self.called.append(('removeWidget', widget))


class QExpanderLabelTest(TestCase):
    """Test the expander labe."""

    @defer.inlineCallbacks
    def setUp(self):
        """Set tests."""
        yield super(QExpanderLabelTest, self).setUp()
        self.label_content = 'test'
        self.label = QExpanderLabel(self.label_content)

    def test_mouse_press_event_arrow_right(self):
        """Test the actions taken when we press the mouse."""
        self.label.arrow = FakeArrow()
        self.label.arrow.direction = QArrow.RIGHT
        self.label.mousePressEvent(None)
        self.assertEqual(QArrow.DOWN, self.label.arrow.direction)
        self.assertIn('repaint', self.label.arrow.called[0])

    def test_mouse_press_event_arrow_down(self):
        """Test the actions taken when we press the mouse."""
        self.label.arrow = FakeArrow()
        self.label.arrow.direction = QArrow.DOWN
        self.label.mousePressEvent(None)
        self.assertEqual(QArrow.RIGHT, self.label.arrow.direction)
        self.assertIn('repaint', self.label.arrow.called[0])

    def test_text(self):
        """Test getting the text."""
        self.fake_label = FakeLabel(self.label_content)
        self.label.label = self.fake_label
        self.assertEqual(self.label_content, self.label.text())
        self.assertIn('text', self.fake_label.called[0])

    def test_set_text(self):
        """Test setting the text."""
        self.fake_label = FakeLabel(self.label_content)
        self.label.label = self.fake_label
        new_label = 'new'
        self.label.setText(new_label)
        self.assertEqual(self.fake_label.content, new_label)
        self.assertIn('setText', self.fake_label.called[0])


class QExpanderTest(TestCase):
    """Test the QExpander widget."""

    @defer.inlineCallbacks
    def setUp(self):
        """Set up tests."""
        yield super(QExpanderTest, self).setUp()
        self.label_content = 'test'
        self.expander = QExpander(self.label_content)

    def test_init(self):
        """Test the constructor."""

    def test_add_widget(self):
        """Test adding a widget."""
        widget = FakeLabel('test')
        self.expander.layout = FakeLayout()
        self.expander.addWidget(widget)
        self.assertIn(('insertWidget', 1, widget), self.expander.layout.called)

    def test_add_widget_present(self):
        """Test adding a widge when one is already present."""
        widget = FakeLabel('test')
        self.expander.layout = FakeLayout()
        self.expander.content = widget
        self.expander.addWidget(widget)
        self.assertIn(('removeWidget', widget), self.expander.layout.called)
        self.assertIn(('insertWidget', 1, widget), self.expander.layout.called)

    def test_add_widget_expanded(self):
        """Test adding a widget when we are expanded."""
        widget = FakeLabel('test')
        self.expander.layout = FakeLayout()
        self.expander._expanded = False
        self.expander.addWidget(widget)
        self.assertIn(('insertWidget', 1, widget), self.expander.layout.called)
        self.assertIn(('setVisible', False), widget.called)

    def test_add_widget_collapsed(self):
        """Test adding a widget when we are collapsed."""
        widget = FakeLabel('test')
        self.expander.layout = FakeLayout()
        self.expander._expanded = True
        self.expander.addWidget(widget)
        self.assertIn(('insertWidget', 1, widget), self.expander.layout.called)
        self.assertIn(('setVisible', True), widget.called)

    def test_text(self):
        """Test getting the text."""
        label_content = 'content'
        fake_label = FakeLabel(label_content)
        self.expander.label = fake_label
        self.assertEqual(self.expander.text(), label_content)
        self.assertIn(('text',), fake_label.called)

    def test_set_text(self):
        """Test setting the text."""
        label_content = 'content'
        fake_label = FakeLabel('')
        self.expander.label = fake_label
        self.expander.setText(label_content)
        self.assertIn(('setText', label_content), fake_label.called)

    def test_expanded(self):
        """Test getting expanded."""
        self.expander._expanded = True
        self.assertEqual(self.expander.expanded(), self.expander._expanded)