aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/tests/signals/signal_emission_gui_test.py
blob: 5a49b9d1262ba962b41165589370705abe35a5a9 (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
#!/usr/bin/env python
# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

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

import gc
import os
import sys
import unittest

from pathlib import Path
sys.path.append(os.fspath(Path(__file__).resolve().parents[1]))
from init_paths import init_test_paths
init_test_paths(False)

from PySide6.QtWidgets import QSpinBox, QPushButton

from helper.basicpyslotcase import BasicPySlotCase
from helper.usesqapplication import UsesQApplication


class ButtonPySlot(UsesQApplication, BasicPySlotCase):
    """Tests the connection of python slots to QPushButton signals"""

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

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


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
        # PYSIDE-535: Need to collect garbage in PyPy to trigger deletion
        gc.collect()
        super(SpinBoxPySlot, self).tearDown()

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

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

    def atestSpinBoxValueChangedFewArgs(self):
        """Emission of signals with fewer arguments than needed"""
        self.spin.valueChanged.connect(self.cb)
        self.args = (554,)
        self.assertRaises(TypeError, self.spin.valueChanged.emit)


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)

        spinSend.valueChanged.connect(spinRec.setValue)
        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)

        spinSend.valueChanged.connect(spinRec.setValue)
        self.assertEqual(spinRec.value(), 5)
        self.assertEqual(spinSend.value(), 42)
        spinSend.valueChanged.emit(3)

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

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


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