7 from common import gobject, testhelper
9 class C(gobject.GObject):
10 __gsignals__ = { 'my_signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
11 (gobject.TYPE_INT,)) }
12 def do_my_signal(self, arg):
16 def do_my_signal(self, arg2):
18 C.do_my_signal(self, arg2)
20 class TestSignalCreation(unittest.TestCase):
22 def test_illegals(self):
23 self.assertRaises(TypeError, lambda: gobject.signal_new('test',
27 (gobject.TYPE_LONG,)))
30 class TestChaining(unittest.TestCase):
33 self.inst.connect("my_signal", self.my_signal_handler_cb, 1, 2, 3)
35 def my_signal_handler_cb(self, *args):
37 assert isinstance(args[0], C)
38 assert args[0] == self.inst
40 assert isinstance(args[1], int)
43 assert args[2:] == (1, 2, 3)
45 def testChaining(self):
46 self.inst.emit("my_signal", 42)
47 assert self.inst.arg == 42
49 def testChaining(self):
51 inst2.emit("my_signal", 44)
52 assert inst2.arg == 44
53 assert inst2.arg2 == 44
55 # This is for bug 153718
56 class TestGSignalsError(unittest.TestCase):
57 def testInvalidType(self, *args):
59 class Foo(gobject.GObject):
61 self.assertRaises(TypeError, foo)
64 def testInvalidName(self, *args):
66 class Foo(gobject.GObject):
67 __gsignals__ = {'not-exists' : 'override'}
68 self.assertRaises(TypeError, foo)
71 class TestGPropertyError(unittest.TestCase):
72 def testInvalidType(self, *args):
74 class Foo(gobject.GObject):
75 __gproperties__ = None
76 self.assertRaises(TypeError, foo)
79 def testInvalidName(self, *args):
81 class Foo(gobject.GObject):
82 __gproperties__ = { None: None }
84 self.assertRaises(TypeError, foo)
87 class TestList(unittest.TestCase):
88 def testListObject(self):
89 self.assertEqual(gobject.signal_list_names(C), ('my-signal',))
92 def my_accumulator(ihint, return_accu, handler_return, user_data):
93 """An accumulator that stops emission when the sum of handler
94 returned values reaches 3"""
95 assert user_data == "accum data"
97 return False, return_accu
98 return True, return_accu + handler_return
100 class Foo(gobject.GObject):
102 'my-acc-signal': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_INT,
103 (), my_accumulator, "accum data"),
104 'my-other-acc-signal': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_BOOLEAN,
105 (), gobject.signal_accumulator_true_handled)
108 class TestAccumulator(unittest.TestCase):
110 def testAccumulator(self):
112 inst.connect("my-acc-signal", lambda obj: 1)
113 inst.connect("my-acc-signal", lambda obj: 2)
114 ## the value returned in the following handler will not be
115 ## considered, because at this point the accumulator already
116 ## reached its limit.
117 inst.connect("my-acc-signal", lambda obj: 3)
118 retval = inst.emit("my-acc-signal")
119 self.assertEqual(retval, 3)
121 def testAccumulatorTrueHandled(self):
123 inst.connect("my-other-acc-signal", self._true_handler1)
124 inst.connect("my-other-acc-signal", self._true_handler2)
125 ## the following handler will not be called because handler2
126 ## returns True, so it should stop the emission.
127 inst.connect("my-other-acc-signal", self._true_handler3)
128 self.__true_val = None
129 inst.emit("my-other-acc-signal")
130 self.assertEqual(self.__true_val, 2)
132 def _true_handler1(self, obj):
135 def _true_handler2(self, obj):
138 def _true_handler3(self, obj):
142 class E(gobject.GObject):
143 __gsignals__ = { 'signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
146 gobject.GObject.__init__(self)
150 assert self.status == 0
153 class F(gobject.GObject):
154 __gsignals__ = { 'signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
157 gobject.GObject.__init__(self)
163 class TestEmissionHook(unittest.TestCase):
167 e.connect('signal', self._callback)
168 gobject.add_emission_hook(E, "signal", self._emission_hook)
170 self.assertEqual(e.status, 3)
172 def testRemove(self):
175 e.connect('signal', self._callback)
176 hook_id = gobject.add_emission_hook(E, "signal", self._emission_hook)
177 gobject.remove_emission_hook(E, "signal", hook_id)
179 self.assertEqual(e.status, 3)
181 def _emission_hook(self, e):
182 self.assertEqual(e.status, 1)
185 def _callback(self, e):
187 self.assertEqual(e.status, 2)
189 self.assertEqual(e.status, 1)
192 def testCallbackReturnFalse(self):
195 def _emission_hook(obj):
198 hook_id = gobject.add_emission_hook(obj, "signal", _emission_hook)
201 self.assertEqual(obj.status, 3)
203 def testCallbackReturnTrue(self):
206 def _emission_hook(obj):
209 hook_id = gobject.add_emission_hook(obj, "signal", _emission_hook)
212 gobject.remove_emission_hook(obj, "signal", hook_id)
213 self.assertEqual(obj.status, 4)
215 def testCallbackReturnTrueButRemove(self):
218 def _emission_hook(obj):
221 hook_id = gobject.add_emission_hook(obj, "signal", _emission_hook)
223 gobject.remove_emission_hook(obj, "signal", hook_id)
225 self.assertEqual(obj.status, 3)
227 class TestClosures(unittest.TestCase):
231 def _callback(self, e):
234 def testDisconnect(self):
236 e.connect('signal', self._callback)
237 e.disconnect_by_func(self._callback)
239 self.assertEqual(self.count, 0)
241 def testHandlerBlock(self):
243 e.connect('signal', self._callback)
244 e.handler_block_by_func(self._callback)
246 self.assertEqual(self.count, 0)
248 def testHandlerUnBlock(self):
250 signal_id = e.connect('signal', self._callback)
251 e.handler_block(signal_id)
252 e.handler_unblock_by_func(self._callback)
254 self.assertEqual(self.count, 1)
256 def testHandlerBlockMethod(self):
262 def callback(self, o):
264 o.handler_block_by_func(self.callback)
268 e.connect("signal", inst.callback)
270 self.assertEqual(inst.a, 1)
273 def testGString(self):
274 class C(gobject.GObject):
275 __gsignals__ = { 'my_signal': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_GSTRING,
276 (gobject.TYPE_GSTRING,)) }
277 def __init__(self, test):
278 gobject.GObject.__init__(self)
280 def do_my_signal(self, data):
282 self.test.assertEqual(len(data), 3)
283 return ''.join([data[2], data[1], data[0]])
285 data = c.emit("my_signal", "\01\00\02")
286 self.assertEqual(data, "\02\00\01")
288 class SigPropClass(gobject.GObject):
289 __gsignals__ = { 'my_signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
290 (gobject.TYPE_INT,)) }
293 'foo': (str, None, None, '', gobject.PARAM_WRITABLE|gobject.PARAM_CONSTRUCT),
296 signal_emission_failed = False
298 def do_my_signal(self, arg):
301 def do_set_property(self, pspec, value):
302 if pspec.name == 'foo':
305 raise AttributeError, 'unknown property %s' % pspec.name
307 self.emit("my-signal", 1)
309 self.signal_emission_failed = True
312 class TestSigProp(unittest.TestCase):
313 def testEmitInPropertySetter(self):
315 self.failIf(obj.signal_emission_failed)
317 f = gobject.SIGNAL_RUN_FIRST
318 l = gobject.SIGNAL_RUN_LAST
319 float = gobject.TYPE_FLOAT
320 double = gobject.TYPE_DOUBLE
321 uint = gobject.TYPE_UINT
322 ulong = gobject.TYPE_ULONG
324 class CM(gobject.GObject):
327 test2=(l, None, (str,)),
328 test3=(l, int, (double,)),
329 test4=(f, None, (bool, long, float, double, int, uint, ulong)),
330 test_float=(l, float, (float,)),
331 test_double=(l, double, (double, )),
332 test_string=(l, str, (str, )),
333 test_object=(l, object, (object, )),
336 class _TestCMarshaller:
339 testhelper.connectcallbacks(self.obj)
342 self.obj.emit("test1")
345 self.obj.emit("test2", "string")
348 rv = self.obj.emit("test3", 42.0)
349 self.assertEqual(rv, 20)
352 self.obj.emit("test4", True, 10L, 3.14, 1.78, 20, 30L, 31L)
354 def testTestReturnFloat(self):
355 rv = self.obj.emit("test-float", 1.234)
356 self.failUnless(rv >= 1.233999 and rv <= 1.2400001, rv)
358 def testTestReturnDouble(self):
359 rv = self.obj.emit("test-double", 1.234)
360 self.assertEqual(rv, 1.234)
362 def testTestReturnString(self):
363 rv = self.obj.emit("test-string", "str")
364 self.assertEqual(rv, "str")
366 def testTestReturnObject(self):
367 rv = self.obj.emit("test-object", self)
368 self.assertEqual(rv, self)
370 if 'generic-c-marshaller' in gobject.features:
371 class TestCMarshaller(_TestCMarshaller, unittest.TestCase):
375 print '** WARNING: LIBFFI disabled, not testing'
379 class TestPyGValue(unittest.TestCase):
380 def testNoneNULLBoxedConversion(self):
381 class C(gobject.GObject):
382 __gsignals__ = dict(my_boxed_signal=(
383 gobject.SIGNAL_RUN_LAST,
384 gobject.type_from_name('GStrv'), ()))
387 obj.connect('my-boxed-signal', lambda obj: None)
389 obj.emit('my-boxed-signal')
390 assert not sys.last_type
392 if __name__ == '__main__':