aboutsummaryrefslogtreecommitdiffstats
path: root/tests/signals/signal_emission_test.py
blob: 57b71948ffd76077566fe796f486e6847ac4be6e (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python

"""Tests covering signal emission and receiving to python slots"""

import sys
import unittest

from PySide.QtCore import QObject, SIGNAL, SLOT, QProcess, QTimeLine
from PySide.QtCore import QTimer, QThread

try:
    from PySide.QtGui import QSpinBox, QPushButton
except ImportError:
    QSpinBox = object
    QPushButton = object
    QApplication = object

from helper import BasicPySlotCase, UsesQApplication, UsesQCoreApplication
from helper.decorators import requires

@requires('PySide.QtGui')
class ButtonPySlot(UsesQApplication, BasicPySlotCase):
    """Tests the connection of python slots to QPushButton signals"""

    def setUp(self):
        super(ButtonPySlot, self).setUp()

    def tearDown(self):
        super(ButtonPySlot, self).setUp()

    def testButtonClicked(self):
        """Connection of a python slot to QPushButton.clicked()"""
        button = QPushButton('Mylabel')
        QObject.connect(button, SIGNAL('clicked()'), self.cb)
        self.args = tuple()
        button.emit(SIGNAL('clicked()'))
        self.assert_(self.called)

    def testButtonClick(self):
        """Indirect qt signal emission using the QPushButton.click() method """
        button = QPushButton('label')
        QObject.connect(button, SIGNAL('clicked()'), self.cb)
        self.args = tuple()
        button.click()
        self.assert_(self.called)


@requires('PySide.QtGui')
class SpinBoxPySlot(UsesQApplication, BasicPySlotCase):
    """Tests the connection of python slots to QSpinBox signals"""

    def setUp(self):
        super(SpinBoxPySlot, self).setUp()
        self.spin = QSpinBox()

    def tearDown(self):
        del self.spin
        super(SpinBoxPySlot, self).tearDown()

    def testSpinBoxValueChanged(self):
        """Connection of a python slot to QSpinBox.valueChanged(int)"""
        QObject.connect(self.spin, SIGNAL('valueChanged(int)'), self.cb)
        self.args = [3]
        self.spin.emit(SIGNAL('valueChanged(int)'), *self.args)
        self.assert_(self.called)

    def testSpinBoxValueChangedImplicit(self):
        """Indirect qt signal emission using QSpinBox.setValue(int)"""
        QObject.connect(self.spin, SIGNAL('valueChanged(int)'), self.cb)
        self.args = [42]
        self.spin.setValue(self.args[0])
        self.assert_(self.called)

    def atestSpinBoxValueChangedFewArgs(self):
        """Emission of signals with fewer arguments than needed"""
        # XXX: PyQt4 crashes on the assertRaises
        QObject.connect(self.spin, SIGNAL('valueChanged(int)'), self.cb)
        self.args = (554,)
        self.assertRaises(TypeError, self.spin.emit, SIGNAL('valueChanged(int)'))

@requires('PySide.QtGui')
class QSpinBoxQtSlots(UsesQApplication):
    """Tests the connection to QSpinBox qt slots"""

    qapplication = True

    def testSetValueIndirect(self):
        """Indirect signal emission: QSpinBox using valueChanged(int)/setValue(int)"""
        spinSend = QSpinBox()
        spinRec = QSpinBox()

        spinRec.setValue(5)

        QObject.connect(spinSend, SIGNAL('valueChanged(int)'), spinRec, SLOT('setValue(int)'))
        self.assertEqual(spinRec.value(), 5)
        spinSend.setValue(3)
        self.assertEqual(spinRec.value(), 3)
        self.assertEqual(spinSend.value(), 3)

    def testSetValue(self):
        """Direct signal emission: QSpinBox using valueChanged(int)/setValue(int)"""
        spinSend = QSpinBox()
        spinRec = QSpinBox()

        spinRec.setValue(5)
        spinSend.setValue(42)

        QObject.connect(spinSend, SIGNAL('valueChanged(int)'), spinRec, SLOT('setValue(int)'))
        self.assertEqual(spinRec.value(), 5)
        self.assertEqual(spinSend.value(), 42)
        spinSend.emit(SIGNAL('valueChanged(int)'), 3)

        self.assertEqual(spinRec.value(), 3)
        #Direct emission shouldn't change the value of the emitter
        self.assertEqual(spinSend.value(), 42)

        spinSend.emit(SIGNAL('valueChanged(int)'), 66)
        self.assertEqual(spinRec.value(), 66)
        self.assertEqual(spinSend.value(), 42)


class ArgsOnEmptySignal(UsesQCoreApplication):
    '''Trying to emit a signal without arguments passing some arguments'''

    def testArgsToNoArgsSignal(self):
        '''Passing arguments to a signal without arguments'''
        process = QProcess()
        self.assertRaises(TypeError, process.emit, SIGNAL('started()'), 42)


class MoreArgsOnEmit(UsesQCoreApplication):
    '''Trying to pass more args than needed to emit (signals with args)'''

    def testMoreArgs(self):
        '''Passing more arguments than needed'''
        process = QProcess()
        self.assertRaises(TypeError, process.emit, SIGNAL('finished(int)'), 55, 55)

class Dummy(QObject):
    '''Dummy class'''
    pass


class PythonSignalToCppSlots(UsesQCoreApplication):
    '''Connect python signals to C++ slots'''

    def testWithoutArgs(self):
        '''Connect python signal to QTimeLine.toggleDirection()'''
        timeline = QTimeLine()
        dummy = Dummy()
        QObject.connect(dummy, SIGNAL('dummy()'),
                        timeline, SLOT('toggleDirection()'))

        orig_dir = timeline.direction()
        dummy.emit(SIGNAL('dummy()'))
        new_dir = timeline.direction()

        if orig_dir == QTimeLine.Forward:
            self.assertEqual(new_dir, QTimeLine.Backward)
        else:
            self.assertEqual(new_dir, QTimeLine.Forward)

    def testWithArgs(self):
        '''Connect python signals to QTimeLine.setCurrentTime(int)'''
        timeline = QTimeLine()
        dummy = Dummy()

        QObject.connect(dummy, SIGNAL('dummy(int)'),
                        timeline, SLOT('setCurrentTime(int)'))

        current = timeline.currentTime()
        dummy.emit(SIGNAL('dummy(int)'), current+42)
        self.assertEqual(timeline.currentTime(), current+42)

class CppSignalsToCppSlots(UsesQCoreApplication):
    '''Connection between C++ slots and signals'''

    def testWithoutArgs(self):
        '''Connect QThread.started() to QTimeLine.togglePaused()'''
        thread = QThread()
        timeline = QTimeLine()

        QObject.connect(thread, SIGNAL('started()'),
                        timeline, SLOT('toggleDirection()'))
        QObject.connect(thread, SIGNAL('started()'),
                        self.exit_app_cb)

        orig_dir = timeline.direction()

        timer = QTimer.singleShot(1000, self.exit_app_cb) # Just for safety

        thread.start()
        self.app.exec_()
        thread.wait()

        new_dir = timeline.direction()

        if orig_dir == QTimeLine.Forward:
            self.assertEqual(new_dir, QTimeLine.Backward)
        else:
            self.assertEqual(new_dir, QTimeLine.Forward)


if __name__ == '__main__':
    unittest.main()