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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
'''Test cases for QObject property and setProperty'''
import unittest
from PySide.QtCore import *
class Dummy(object):
'''Pure python sample class'''
pass
class MySize(QSize):
'''Extended class'''
pass
class ExQObject(QObject):
def __init__(self, *args, **kargs):
QObject.__init__(self, *args, **kargs)
def setProperty(self, value):
self._value = value
def getProperty(self):
return self._value
registeredproperty = Property(int, getProperty, setProperty)
class MyObject(QObject):
'''Test Property'''
def readPP(self):
return 42
def trySetPP(self):
self.pp = 0
pp = Property(int, readPP, constant=True)
class MySubObject(MyObject):
pass
class MyMultipleObject(MyObject, object):
pass
class MyObjectWithNotifyProperty(QObject):
def __init__(self, parent=None):
QObject.__init__(self, parent)
self.p = 0
def readP(self):
return self.p
def writeP(self, v):
self.p = v
self.notifyP.emit()
notifyP = Signal()
myProperty = Property(int, readP, fset=writeP, notify=notifyP)
class PropertyCase(unittest.TestCase):
'''Test case for QObject properties'''
def testObjectNameProperty(self):
#QObject.setProperty() for existing C++ property
obj = QObject()
self.assert_(obj.setProperty('objectName', 'dummy'))
self.assertEqual(obj.objectName(), 'dummy')
self.assert_(obj.setProperty('objectName', 'foobar'))
self.assertEqual(obj.objectName(), 'foobar')
def testDynamicProperty(self):
#QObject.setProperty() for dynamic properties
obj = QObject()
# Should return false when creating a new dynamic property
self.assert_(not obj.setProperty('dummy', 'mydata'))
prop = obj.property('dummy')
self.assert_(isinstance(prop, unicode))
self.assertEqual(obj.property('dummy'), 'mydata')
self.assert_(not obj.setProperty('dummy', 'zigzag'))
prop = obj.property('dummy')
self.assert_(isinstance(prop, unicode))
self.assertEqual(obj.property('dummy'), 'zigzag')
self.assert_(not obj.setProperty('dummy', 42))
prop = obj.property('dummy')
self.assert_(isinstance(prop, int))
# QVariant.toInt has a bool* arg in C++, so returns a tuple
self.assertEqual(obj.property('dummy'), 42)
def testStringProperty(self):
obj = QObject()
self.assert_(not obj.setProperty('dummy', 'data'))
prop = obj.property('dummy')
self.assert_(isinstance(prop, unicode))
self.assertEqual(obj.property('dummy'), 'data')
def testImplicitQVariantProperty(self):
obj = QObject()
self.assert_(not obj.setProperty('dummy', 'data'))
prop = obj.property('dummy')
self.assert_(isinstance(prop, unicode))
self.assertEqual(obj.property('dummy'), 'data')
def testInvalidProperty(self):
#QObject.property() for invalid properties
obj = QObject()
prop = obj.property('dummy')
self.assertEqual(prop, None)
def testTypeNamePythonClasses(self):
'''QVariant of pure python classes'''
d = Dummy()
obj = QObject()
obj.setProperty('foo', d)
# inherited type name from other binding
self.assertEqual(obj.property('foo'), d)
def testQVariantPyList(self):
'''QVariant(QVariantList).toPyObject() equals original list'''
obj = QObject()
obj.setProperty('foo', [1, 'two', 3])
self.assertEqual(obj.property('foo'), [1, 'two', 3])
def testSubClassConvertion(self):
'''QVariant(QSize subclass) type is UserType and returns same object'''
mysize = MySize(0, 0)
obj = QObject()
obj.setProperty('foo', mysize)
self.assertTrue(obj.property('foo') is mysize)
def testValueType(self):
rect = QRect(1, 2, 3, 4)
obj = QObject()
obj.setProperty('rect', rect)
'''Value types when converted to QVariant is copyed'''
self.assertFalse(obj.property('rect') is rect)
self.assertEqual(obj.property('rect'), rect)
def testSubClassProperty(self):
o = MyObject()
self.assertEqual(o.property('pp'), 42)
so = MySubObject()
self.assertEqual(so.property('pp'), 42)
mo = MyMultipleObject()
self.assertEqual(mo.property('pp'), 42)
class PropertyWithConstructorCase(unittest.TestCase):
'''Test case for QObject properties set using named arguments in the constructor.'''
def testObjectNameProperty(self):
#QObject(property=value) for existing C++ property
obj = QObject(objectName='dummy')
self.assertEqual(obj.objectName(), 'dummy')
def testDynamicPropertyRaisesException(self):
self.assertRaises(AttributeError, QObject, dummy=42)
def testPythonDeclaredProperty(self):
obj = ExQObject(registeredproperty=123)
self.assertEqual(obj.registeredproperty, 123)
def testPythonDeclaredPropertyNoSetted(self):
try:
obj = ExQObject()
a = obj.registeredproperty
except AttributeError:
pass
def testConstructorPropertyInQObjectDerived(self):
#QTimer(property=value) for existing C++ property
obj = QTimer(objectName='dummy')
self.assertEqual(obj.objectName(), 'dummy')
def testReadOnlyPythonProperty(self):
o = MyObject()
self.assertEqual(o.pp, 42)
self.assertRaises(AttributeError, o.trySetPP)
class PropertyWithNotify(unittest.TestCase):
def called(self):
self.called_ = True
def testMetaData(self):
obj = MyObjectWithNotifyProperty()
mo = obj.metaObject()
self.assertEqual(mo.propertyCount(), 2)
p = mo.property(1)
self.assertEqual(p.name(), "myProperty")
self.assert_(p.hasNotifySignal())
def testNotify(self):
self.called_ = False
obj = MyObjectWithNotifyProperty()
obj.notifyP.connect(self.called)
obj.myProperty = 10
self.assert_(self.called_)
class MetaPropertyTest(unittest.TestCase):
def testConstant(self):
obj = MyObject()
mo = obj.metaObject()
self.assertEqual(mo.propertyCount(), 2)
p = mo.property(1)
self.assertTrue(p.isConstant())
obj = MyObjectWithNotifyProperty()
mo = obj.metaObject()
self.assertEqual(mo.propertyCount(), 2)
p = mo.property(1)
self.assertFalse(p.isConstant())
if __name__ == '__main__':
unittest.main()
|