1#!/usr/bin/env python
2
3#############################################################################
4##
5## Copyright (C) 2016 The Qt Company Ltd.
6## Contact: https://www.qt.io/licensing/
7##
8## This file is part of the test suite of Qt for Python.
9##
10## $QT_BEGIN_LICENSE:GPL-EXCEPT$
11## Commercial License Usage
12## Licensees holding valid commercial Qt licenses may use this file in
13## accordance with the commercial license agreement provided with the
14## Software or, alternatively, in accordance with the terms contained in
15## a written agreement between you and The Qt Company. For licensing terms
16## and conditions see https://www.qt.io/terms-conditions. For further
17## information use the contact form at https://www.qt.io/contact-us.
18##
19## GNU General Public License Usage
20## Alternatively, this file may be used under the terms of the GNU
21## General Public License version 3 as published by the Free Software
22## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
23## included in the packaging of this file. Please review the following
24## information to ensure the GNU General Public License requirements will
25## be met: https://www.gnu.org/licenses/gpl-3.0.html.
26##
27## $QT_END_LICENSE$
28##
29#############################################################################
30
31import os
32import sys
33import unittest
34
35sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
36from init_paths import init_test_paths
37init_test_paths(False)
38
39from PySide2.QtCore import QTimer, Signal, QObject, Slot, Qt
40from helper.usesqcoreapplication import UsesQCoreApplication
41
42class MyObject(QTimer):
43    sig1 = Signal()
44    sig2 = Signal(int, name='rangeChanged')
45    sig3 = Signal(int)
46    sig4 = Signal((int,), (str,))
47    sig5 = Signal((str,), (int,))
48    sig6 = Signal(QObject)
49
50    @Slot(int)
51    def myRange(self, r):
52        self._range = r
53
54    def slot1(self):
55        self._called = True
56
57    def slotString(self, s):
58        self._s = s
59
60    def slotObject(self, o):
61        self._o = o
62
63
64class SignalObjectTest(UsesQCoreApplication):
65    def cb(self):
66        self._cb_called = True
67        self.app.exit()
68
69    def testsingleConnect(self):
70        o = MyObject()
71        o.sig1.connect(o.slot1)
72        o.sig1.emit()
73        self.assertTrue(o._called)
74
75    def testSignalWithArgs(self):
76        o = MyObject()
77        o.sig3.connect(o.myRange)
78        o.sig3.emit(10)
79        self.assertEqual(o._range, 10)
80
81    def testSignatureParse(self):
82        o = MyObject()
83        o.sig2.connect(o.myRange)
84        o.sig2.emit(10)
85
86    def testDictOperator(self):
87        o = MyObject()
88        o.sig4[str].connect(o.slotString)
89        o.sig4[str].emit("PySide")
90        self.assertEqual(o._s, "PySide")
91
92    def testGeneretedSignal(self):
93        o = MyObject()
94        o.timeout.connect(self.cb)
95        o.start(100)
96        self.app.exec_()
97        self.assertTrue(self._cb_called)
98
99    def testConnectionType(self):
100        o = MyObject()
101        o.timeout.connect(self.cb, type=Qt.DirectConnection)
102        o.start(100)
103        self.app.exec_()
104        self.assertTrue(self._cb_called)
105
106    def testSignalWithSignal(self):
107        o = MyObject()
108        o.sig2.connect(o.myRange)
109        o.sig5.connect(o.sig2)
110        o.sig5[int].emit(10)
111        self.assertEqual(o._range, 10)
112
113    def testSignalWithObject(self):
114        o = MyObject()
115        o.sig6.connect(o.slotObject)
116        arg = QObject()
117        o.sig6.emit(arg)
118        self.assertEqual(arg, o._o)
119
120if __name__ == '__main__':
121    unittest.main()
122