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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
# -*- Mode: Python -*-
import gc
import unittest
import sys
from common import gobject, testhelper
class C(gobject.GObject):
__gsignals__ = { 'my_signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
(gobject.TYPE_INT,)) }
def do_my_signal(self, arg):
self.arg = arg
class D(C):
def do_my_signal(self, arg2):
self.arg2 = arg2
C.do_my_signal(self, arg2)
class TestSignalCreation(unittest.TestCase):
# Bug 540376.
def test_illegals(self):
self.assertRaises(TypeError, lambda: gobject.signal_new('test',
None,
0,
gobject.TYPE_NONE,
(gobject.TYPE_LONG,)))
class TestChaining(unittest.TestCase):
def setUp(self):
self.inst = C()
self.inst.connect("my_signal", self.my_signal_handler_cb, 1, 2, 3)
def my_signal_handler_cb(self, *args):
assert len(args) == 5
assert isinstance(args[0], C)
assert args[0] == self.inst
assert isinstance(args[1], int)
assert args[1] == 42
assert args[2:] == (1, 2, 3)
def testChaining(self):
self.inst.emit("my_signal", 42)
assert self.inst.arg == 42
def testChaining(self):
inst2 = D()
inst2.emit("my_signal", 44)
assert inst2.arg == 44
assert inst2.arg2 == 44
# This is for bug 153718
class TestGSignalsError(unittest.TestCase):
def testInvalidType(self, *args):
def foo():
class Foo(gobject.GObject):
__gsignals__ = None
self.assertRaises(TypeError, foo)
gc.collect()
def testInvalidName(self, *args):
def foo():
class Foo(gobject.GObject):
__gsignals__ = {'not-exists' : 'override'}
self.assertRaises(TypeError, foo)
gc.collect()
class TestGPropertyError(unittest.TestCase):
def testInvalidType(self, *args):
def foo():
class Foo(gobject.GObject):
__gproperties__ = None
self.assertRaises(TypeError, foo)
gc.collect()
def testInvalidName(self, *args):
def foo():
class Foo(gobject.GObject):
__gproperties__ = { None: None }
self.assertRaises(TypeError, foo)
gc.collect()
class TestList(unittest.TestCase):
def testListObject(self):
self.assertEqual(gobject.signal_list_names(C), ('my-signal',))
def my_accumulator(ihint, return_accu, handler_return, user_data):
"""An accumulator that stops emission when the sum of handler
returned values reaches 3"""
assert user_data == "accum data"
if return_accu >= 3:
return False, return_accu
return True, return_accu + handler_return
class Foo(gobject.GObject):
__gsignals__ = {
'my-acc-signal': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_INT,
(), my_accumulator, "accum data"),
'my-other-acc-signal': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_BOOLEAN,
(), gobject.signal_accumulator_true_handled)
}
class TestAccumulator(unittest.TestCase):
def testAccumulator(self):
inst = Foo()
inst.connect("my-acc-signal", lambda obj: 1)
inst.connect("my-acc-signal", lambda obj: 2)
## the value returned in the following handler will not be
## considered, because at this point the accumulator already
## reached its limit.
inst.connect("my-acc-signal", lambda obj: 3)
retval = inst.emit("my-acc-signal")
self.assertEqual(retval, 3)
def testAccumulatorTrueHandled(self):
inst = Foo()
inst.connect("my-other-acc-signal", self._true_handler1)
inst.connect("my-other-acc-signal", self._true_handler2)
## the following handler will not be called because handler2
## returns True, so it should stop the emission.
inst.connect("my-other-acc-signal", self._true_handler3)
self.__true_val = None
inst.emit("my-other-acc-signal")
self.assertEqual(self.__true_val, 2)
def _true_handler1(self, obj):
self.__true_val = 1
return False
def _true_handler2(self, obj):
self.__true_val = 2
return True
def _true_handler3(self, obj):
self.__true_val = 3
return False
class E(gobject.GObject):
__gsignals__ = { 'signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
()) }
def __init__(self):
gobject.GObject.__init__(self)
self.status = 0
def do_signal(self):
assert self.status == 0
self.status = 1
class F(gobject.GObject):
__gsignals__ = { 'signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
()) }
def __init__(self):
gobject.GObject.__init__(self)
self.status = 0
def do_signal(self):
self.status += 1
class TestEmissionHook(unittest.TestCase):
def testAdd(self):
self.hook = True
e = E()
e.connect('signal', self._callback)
gobject.add_emission_hook(E, "signal", self._emission_hook)
e.emit('signal')
self.assertEqual(e.status, 3)
def testRemove(self):
self.hook = False
e = E()
e.connect('signal', self._callback)
hook_id = gobject.add_emission_hook(E, "signal", self._emission_hook)
gobject.remove_emission_hook(E, "signal", hook_id)
e.emit('signal')
self.assertEqual(e.status, 3)
def _emission_hook(self, e):
self.assertEqual(e.status, 1)
e.status = 2
def _callback(self, e):
if self.hook:
self.assertEqual(e.status, 2)
else:
self.assertEqual(e.status, 1)
e.status = 3
def testCallbackReturnFalse(self):
self.hook = False
obj = F()
def _emission_hook(obj):
obj.status += 1
return False
hook_id = gobject.add_emission_hook(obj, "signal", _emission_hook)
obj.emit('signal')
obj.emit('signal')
self.assertEqual(obj.status, 3)
def testCallbackReturnTrue(self):
self.hook = False
obj = F()
def _emission_hook(obj):
obj.status += 1
return True
hook_id = gobject.add_emission_hook(obj, "signal", _emission_hook)
obj.emit('signal')
obj.emit('signal')
gobject.remove_emission_hook(obj, "signal", hook_id)
self.assertEqual(obj.status, 4)
def testCallbackReturnTrueButRemove(self):
self.hook = False
obj = F()
def _emission_hook(obj):
obj.status += 1
return True
hook_id = gobject.add_emission_hook(obj, "signal", _emission_hook)
obj.emit('signal')
gobject.remove_emission_hook(obj, "signal", hook_id)
obj.emit('signal')
self.assertEqual(obj.status, 3)
class TestClosures(unittest.TestCase):
def setUp(self):
self.count = 0
def _callback(self, e):
self.count += 1
def testDisconnect(self):
e = E()
e.connect('signal', self._callback)
e.disconnect_by_func(self._callback)
e.emit('signal')
self.assertEqual(self.count, 0)
def testHandlerBlock(self):
e = E()
e.connect('signal', self._callback)
e.handler_block_by_func(self._callback)
e.emit('signal')
self.assertEqual(self.count, 0)
def testHandlerUnBlock(self):
e = E()
signal_id = e.connect('signal', self._callback)
e.handler_block(signal_id)
e.handler_unblock_by_func(self._callback)
e.emit('signal')
self.assertEqual(self.count, 1)
def testHandlerBlockMethod(self):
# Filed as #375589
class A:
def __init__(self):
self.a = 0
def callback(self, o):
self.a = 1
o.handler_block_by_func(self.callback)
inst = A()
e = E()
e.connect("signal", inst.callback)
e.emit('signal')
self.assertEqual(inst.a, 1)
gc.collect()
def testGString(self):
class C(gobject.GObject):
__gsignals__ = { 'my_signal': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_GSTRING,
(gobject.TYPE_GSTRING,)) }
def __init__(self, test):
gobject.GObject.__init__(self)
self.test = test
def do_my_signal(self, data):
self.data = data
self.test.assertEqual(len(data), 3)
return ''.join([data[2], data[1], data[0]])
c = C(self)
data = c.emit("my_signal", "\01\00\02")
self.assertEqual(data, "\02\00\01")
class SigPropClass(gobject.GObject):
__gsignals__ = { 'my_signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
(gobject.TYPE_INT,)) }
__gproperties__ = {
'foo': (str, None, None, '', gobject.PARAM_WRITABLE|gobject.PARAM_CONSTRUCT),
}
signal_emission_failed = False
def do_my_signal(self, arg):
self.arg = arg
def do_set_property(self, pspec, value):
if pspec.name == 'foo':
self._foo = value
else:
raise AttributeError, 'unknown property %s' % pspec.name
try:
self.emit("my-signal", 1)
except TypeError:
self.signal_emission_failed = True
class TestSigProp(unittest.TestCase):
def testEmitInPropertySetter(self):
obj = SigPropClass()
self.failIf(obj.signal_emission_failed)
f = gobject.SIGNAL_RUN_FIRST
l = gobject.SIGNAL_RUN_LAST
float = gobject.TYPE_FLOAT
double = gobject.TYPE_DOUBLE
uint = gobject.TYPE_UINT
ulong = gobject.TYPE_ULONG
class CM(gobject.GObject):
__gsignals__ = dict(
test1=(f, None, ()),
test2=(l, None, (str,)),
test3=(l, int, (double,)),
test4=(f, None, (bool, long, float, double, int, uint, ulong)),
test_float=(l, float, (float,)),
test_double=(l, double, (double, )),
test_string=(l, str, (str, )),
test_object=(l, object, (object, )),
)
class _TestCMarshaller:
def setUp(self):
self.obj = CM()
testhelper.connectcallbacks(self.obj)
def testTest1(self):
self.obj.emit("test1")
def testTest2(self):
self.obj.emit("test2", "string")
def testTest3(self):
rv = self.obj.emit("test3", 42.0)
self.assertEqual(rv, 20)
def testTest4(self):
self.obj.emit("test4", True, 10L, 3.14, 1.78, 20, 30L, 31L)
def testTestReturnFloat(self):
rv = self.obj.emit("test-float", 1.234)
self.failUnless(rv >= 1.233999 and rv <= 1.2400001, rv)
def testTestReturnDouble(self):
rv = self.obj.emit("test-double", 1.234)
self.assertEqual(rv, 1.234)
def testTestReturnString(self):
rv = self.obj.emit("test-string", "str")
self.assertEqual(rv, "str")
def testTestReturnObject(self):
rv = self.obj.emit("test-object", self)
self.assertEqual(rv, self)
if 'generic-c-marshaller' in gobject.features:
class TestCMarshaller(_TestCMarshaller, unittest.TestCase):
pass
else:
print
print '** WARNING: LIBFFI disabled, not testing'
print
# Test for 374653
class TestPyGValue(unittest.TestCase):
def testNoneNULLBoxedConversion(self):
class C(gobject.GObject):
__gsignals__ = dict(my_boxed_signal=(
gobject.SIGNAL_RUN_LAST,
gobject.type_from_name('GStrv'), ()))
obj = C()
obj.connect('my-boxed-signal', lambda obj: None)
sys.last_type = None
obj.emit('my-boxed-signal')
assert not sys.last_type
if __name__ == '__main__':
unittest.main()
|