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 31"""Tests covering signal emission and receiving to python slots""" 32 33import os 34import sys 35import unittest 36 37sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 38from init_paths import init_test_paths 39init_test_paths(False) 40 41from PySide2.QtCore import QObject, SIGNAL, Slot 42from helper.usesqcoreapplication import UsesQCoreApplication 43 44class MyObject(QObject): 45 def __init__(self, parent=None): 46 QObject.__init__(self, parent) 47 self._slotCalledCount = 0 48 49 # this '@Slot()' is needed to get the right sort order in testSharedSignalEmission. 50 # For some reason, it also makes the tests actually work! 51 @Slot() 52 def mySlot(self): 53 self._slotCalledCount = self._slotCalledCount + 1 54 55 56class StaticMetaObjectTest(UsesQCoreApplication): 57 58 def testSignalPropagation(self): 59 o = MyObject() 60 o2 = MyObject() 61 62 # SIGNAL foo not created yet 63 self.assertEqual(o.metaObject().indexOfSignal("foo()"), -1) 64 65 o.connect(SIGNAL("foo()"), o2.mySlot) 66 # SIGNAL foo create after connect 67 self.assertTrue(o.metaObject().indexOfSignal("foo()") > 0) 68 69 # SIGNAL does not propagate to others objects of the same type 70 self.assertEqual(o2.metaObject().indexOfSignal("foo()"), -1) 71 72 del o 73 del o2 74 o = MyObject() 75 # The SIGNAL was destroyed with old objects 76 self.assertEqual(o.metaObject().indexOfSignal("foo()"), -1) 77 78 79 def testSharedSignalEmission(self): 80 o = QObject() 81 m = MyObject() 82 83 o.connect(SIGNAL("foo2()"), m.mySlot) 84 m.connect(SIGNAL("foo2()"), m.mySlot) 85 o.emit(SIGNAL("foo2()")) 86 self.assertEqual(m._slotCalledCount, 1) 87 del o 88 m.emit(SIGNAL("foo2()")) 89 self.assertEqual(m._slotCalledCount, 2) 90 91if __name__ == '__main__': 92 unittest.main() 93