diff options
Diffstat (limited to 'sources/pyside6/tests/signals/signal_emission_gui_test.py')
-rw-r--r-- | sources/pyside6/tests/signals/signal_emission_gui_test.py | 225 |
1 files changed, 97 insertions, 128 deletions
diff --git a/sources/pyside6/tests/signals/signal_emission_gui_test.py b/sources/pyside6/tests/signals/signal_emission_gui_test.py index f4aa7c856..5a49b9d12 100644 --- a/sources/pyside6/tests/signals/signal_emission_gui_test.py +++ b/sources/pyside6/tests/signals/signal_emission_gui_test.py @@ -1,35 +1,10 @@ #!/usr/bin/env python - -############################################################################# -## -## Copyright (C) 2016 The Qt Company Ltd. -## Contact: https://www.qt.io/licensing/ -## -## This file is part of the test suite of Qt for Python. -## -## $QT_BEGIN_LICENSE:GPL-EXCEPT$ -## Commercial License Usage -## Licensees holding valid commercial Qt licenses may use this file in -## accordance with the commercial license agreement provided with the -## Software or, alternatively, in accordance with the terms contained in -## a written agreement between you and The Qt Company. For licensing terms -## and conditions see https://www.qt.io/terms-conditions. For further -## information use the contact form at https://www.qt.io/contact-us. -## -## GNU General Public License Usage -## Alternatively, this file may be used under the terms of the GNU -## General Public License version 3 as published by the Free Software -## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -## included in the packaging of this file. Please review the following -## information to ensure the GNU General Public License requirements will -## be met: https://www.gnu.org/licenses/gpl-3.0.html. -## -## $QT_END_LICENSE$ -## -############################################################################# +# 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 @@ -39,110 +14,104 @@ 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 QObject, SIGNAL, SLOT - -try: - from PySide6.QtWidgets import QSpinBox, QPushButton - hasQtGui = True -except ImportError: - hasQtGui = False +from PySide6.QtWidgets import QSpinBox, QPushButton from helper.basicpyslotcase import BasicPySlotCase from helper.usesqapplication import UsesQApplication -if hasQtGui: - 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') - QObject.connect(button, SIGNAL('clicked()'), self.cb) - self.args = tuple() - button.emit(SIGNAL('clicked(bool)'), False) - self.assertTrue(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.assertTrue(self.called) - - -if hasQtGui: - 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.assertTrue(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.assertTrue(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)')) - -if hasQtGui: - 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 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__': |