aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/tests/signals/lambda_test.py
blob: 23fcdf5fab43e52dea5ed2ad974169faff164f7d (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
#!/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

'''Connecting lambda to signals'''

import os
import sys
import unittest
import weakref

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.QtCore import QCoreApplication, QObject, Signal, SIGNAL, QProcess

from helper.usesqapplication import UsesQApplication


class Sender(QObject):
    void_signal = Signal()
    int_signal = Signal(int)

    def __init__(self, parent=None):
        super().__init__(parent)
        self._delayed_int = 0

    def emit_void(self):
        self.void_signal.emit()

    def emit_int(self, v):
        self.int_signal.emit(v)


class Receiver(QObject):

    def __init__(self, *args):
        super().__init__(*args)


class BasicCase(unittest.TestCase):

    def testSimplePythonSignalNoArgs(self):
        # Connecting a lambda to a simple python signal without arguments
        receiver = Receiver()
        sender = Sender()
        sender.void_signal.connect(lambda: setattr(receiver, 'called', True))
        sender.emit_void()
        self.assertTrue(receiver.called)

    def testSimplePythonSignal(self):
        # Connecting a lambda to a simple python signal witharguments
        receiver = Receiver()
        sender = Sender()
        arg = 42
        sender.int_signal.connect(lambda x: setattr(receiver, 'arg', arg))
        sender.emit_int(arg)
        self.assertEqual(receiver.arg, arg)

    def testSimplePythonSignalNoArgsString(self):
        # Connecting a lambda to a simple python signal without arguments
        receiver = Receiver()
        sender = Sender()
        QObject.connect(sender, SIGNAL('void_signal()'),
                        lambda: setattr(receiver, 'called', True))
        sender.emit_void()
        self.assertTrue(receiver.called)

    def testSimplePythonSignalString(self):
        # Connecting a lambda to a simple python signal witharguments
        receiver = Receiver()
        sender = Sender()
        arg = 42
        QObject.connect(sender, SIGNAL('int_signal(int)'),
                        lambda x: setattr(receiver, 'arg', arg))
        sender.emit_int(arg)
        self.assertEqual(receiver.arg, arg)


class QtSigLambda(UsesQApplication):

    qapplication = True

    def testWithArgs(self):
        '''Connecting a lambda to a signal with and without arguments'''
        proc = QProcess()
        dummy = Receiver()
        proc.started.connect(lambda: setattr(dummy, 'called', True))
        proc.finished.connect(lambda x: setattr(dummy, 'exit_code', x))

        proc.start(sys.executable, ['-c', '""'])
        self.assertTrue(proc.waitForStarted())
        self.assertTrue(proc.waitForFinished())

        self.assertTrue(dummy.called)
        self.assertEqual(dummy.exit_code, proc.exitCode())

    def testRelease(self):
        """PYSIDE-2646: Test whether main thread target slot lambda/methods
           (and their captured objects) are released by the signal manager
           after a while."""

        def do_connect(sender):
            receiver = Receiver()
            sender.void_signal.connect(lambda: setattr(receiver, 'called', True))
            return receiver

        sender = Sender()
        receiver = weakref.ref(do_connect(sender))
        sender.emit_void()
        self.assertTrue(receiver().called)
        del sender
        for i in range(3):
            if not receiver():
                break
            QCoreApplication.processEvents()
        self.assertFalse(receiver())


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