9 from compathelper import _long
11 class C(gobject.GObject):
12 __gsignals__ = { 'my_signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
13 (gobject.TYPE_INT,)) }
14 def do_my_signal(self, arg):
18 def do_my_signal(self, arg2):
20 C.do_my_signal(self, arg2)
22 class TestSignalCreation(unittest.TestCase):
24 def test_illegals(self):
25 self.assertRaises(TypeError, lambda: gobject.signal_new('test',
29 (gobject.TYPE_LONG,)))
32 class TestChaining(unittest.TestCase):
35 self.inst.connect("my_signal", self.my_signal_handler_cb, 1, 2, 3)
37 def my_signal_handler_cb(self, *args):
39 assert isinstance(args[0], C)
40 assert args[0] == self.inst
42 assert isinstance(args[1], int)
45 assert args[2:] == (1, 2, 3)
47 def testChaining(self):
48 self.inst.emit("my_signal", 42)
49 assert self.inst.arg == 42
51 def testChaining(self):
53 inst2.emit("my_signal", 44)
54 assert inst2.arg == 44
55 assert inst2.arg2 == 44
57 # This is for bug 153718
58 class TestGSignalsError(unittest.TestCase):
59 def testInvalidType(self, *args):
61 class Foo(gobject.GObject):
63 self.assertRaises(TypeError, foo)
66 def testInvalidName(self, *args):
68 class Foo(gobject.GObject):
69 __gsignals__ = {'not-exists' : 'override'}
70 self.assertRaises(TypeError, foo)
73 class TestGPropertyError(unittest.TestCase):
74 def testInvalidType(self, *args):
76 class Foo(gobject.GObject):
77 __gproperties__ = None
78 self.assertRaises(TypeError, foo)
81 def testInvalidName(self, *args):
83 class Foo(gobject.GObject):
84 __gproperties__ = { None: None }
86 self.assertRaises(TypeError, foo)
89 class TestList(unittest.TestCase):
90 def testListObject(self):
91 self.assertEqual(gobject.signal_list_names(C), ('my-signal',))
94 def my_accumulator(ihint, return_accu, handler_return, user_data):
95 """An accumulator that stops emission when the sum of handler
96 returned values reaches 3"""
97 assert user_data == "accum data"
99 return False, return_accu
100 return True, return_accu + handler_return
102 class Foo(gobject.GObject):
104 'my-acc-signal': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_INT,
105 (), my_accumulator, "accum data"),
106 'my-other-acc-signal': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_BOOLEAN,
107 (), gobject.signal_accumulator_true_handled)
110 class TestAccumulator(unittest.TestCase):
112 def testAccumulator(self):
114 inst.connect("my-acc-signal", lambda obj: 1)
115 inst.connect("my-acc-signal", lambda obj: 2)
116 ## the value returned in the following handler will not be
117 ## considered, because at this point the accumulator already
118 ## reached its limit.
119 inst.connect("my-acc-signal", lambda obj: 3)
120 retval = inst.emit("my-acc-signal")
121 self.assertEqual(retval, 3)
123 def testAccumulatorTrueHandled(self):
125 inst.connect("my-other-acc-signal", self._true_handler1)
126 inst.connect("my-other-acc-signal", self._true_handler2)
127 ## the following handler will not be called because handler2
128 ## returns True, so it should stop the emission.
129 inst.connect("my-other-acc-signal", self._true_handler3)
130 self.__true_val = None
131 inst.emit("my-other-acc-signal")
132 self.assertEqual(self.__true_val, 2)
134 def _true_handler1(self, obj):
137 def _true_handler2(self, obj):
140 def _true_handler3(self, obj):
144 class E(gobject.GObject):
145 __gsignals__ = { 'signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
148 gobject.GObject.__init__(self)
152 assert self.status == 0
155 class F(gobject.GObject):
156 __gsignals__ = { 'signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
159 gobject.GObject.__init__(self)
165 class TestEmissionHook(unittest.TestCase):
169 e.connect('signal', self._callback)
170 gobject.add_emission_hook(E, "signal", self._emission_hook)
172 self.assertEqual(e.status, 3)
174 def testRemove(self):
177 e.connect('signal', self._callback)
178 hook_id = gobject.add_emission_hook(E, "signal", self._emission_hook)
179 gobject.remove_emission_hook(E, "signal", hook_id)
181 self.assertEqual(e.status, 3)
183 def _emission_hook(self, e):
184 self.assertEqual(e.status, 1)
187 def _callback(self, e):
189 self.assertEqual(e.status, 2)
191 self.assertEqual(e.status, 1)
194 def testCallbackReturnFalse(self):
197 def _emission_hook(obj):
200 hook_id = gobject.add_emission_hook(obj, "signal", _emission_hook)
203 self.assertEqual(obj.status, 3)
205 def testCallbackReturnTrue(self):
208 def _emission_hook(obj):
211 hook_id = gobject.add_emission_hook(obj, "signal", _emission_hook)
214 gobject.remove_emission_hook(obj, "signal", hook_id)
215 self.assertEqual(obj.status, 4)
217 def testCallbackReturnTrueButRemove(self):
220 def _emission_hook(obj):
223 hook_id = gobject.add_emission_hook(obj, "signal", _emission_hook)
225 gobject.remove_emission_hook(obj, "signal", hook_id)
227 self.assertEqual(obj.status, 3)
229 class TestClosures(unittest.TestCase):
233 def _callback(self, e):
236 def testDisconnect(self):
238 e.connect('signal', self._callback)
239 e.disconnect_by_func(self._callback)
241 self.assertEqual(self.count, 0)
243 def testHandlerBlock(self):
245 e.connect('signal', self._callback)
246 e.handler_block_by_func(self._callback)
248 self.assertEqual(self.count, 0)
250 def testHandlerUnBlock(self):
252 signal_id = e.connect('signal', self._callback)
253 e.handler_block(signal_id)
254 e.handler_unblock_by_func(self._callback)
256 self.assertEqual(self.count, 1)
258 def testHandlerBlockMethod(self):
264 def callback(self, o):
266 o.handler_block_by_func(self.callback)
270 e.connect("signal", inst.callback)
272 self.assertEqual(inst.a, 1)
275 def testGString(self):
276 class C(gobject.GObject):
277 __gsignals__ = { 'my_signal': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_GSTRING,
278 (gobject.TYPE_GSTRING,)) }
279 def __init__(self, test):
280 gobject.GObject.__init__(self)
282 def do_my_signal(self, data):
284 self.test.assertEqual(len(data), 3)
285 return ''.join([data[2], data[1], data[0]])
287 data = c.emit("my_signal", "\01\00\02")
288 self.assertEqual(data, "\02\00\01")
290 class SigPropClass(gobject.GObject):
291 __gsignals__ = { 'my_signal': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
292 (gobject.TYPE_INT,)) }
295 'foo': (str, None, None, '', gobject.PARAM_WRITABLE|gobject.PARAM_CONSTRUCT),
298 signal_emission_failed = False
300 def do_my_signal(self, arg):
303 def do_set_property(self, pspec, value):
304 if pspec.name == 'foo':
307 raise AttributeError('unknown property %s' % pspec.name)
309 self.emit("my-signal", 1)
311 self.signal_emission_failed = True
314 class TestSigProp(unittest.TestCase):
315 def testEmitInPropertySetter(self):
317 self.failIf(obj.signal_emission_failed)
319 f = gobject.SIGNAL_RUN_FIRST
320 l = gobject.SIGNAL_RUN_LAST
321 float = gobject.TYPE_FLOAT
322 double = gobject.TYPE_DOUBLE
323 uint = gobject.TYPE_UINT
324 ulong = gobject.TYPE_ULONG
326 class CM(gobject.GObject):
329 test2=(l, None, (str,)),
330 test3=(l, int, (double,)),
331 test4=(f, None, (bool, _long, float, double, int, uint, ulong)),
332 test_float=(l, float, (float,)),
333 test_double=(l, double, (double, )),
334 test_string=(l, str, (str, )),
335 test_object=(l, object, (object, )),
338 class _TestCMarshaller:
341 testhelper.connectcallbacks(self.obj)
344 self.obj.emit("test1")
347 self.obj.emit("test2", "string")
350 rv = self.obj.emit("test3", 42.0)
351 self.assertEqual(rv, 20)
354 self.obj.emit("test4", True, _long(10), 3.14, 1.78, 20, _long(30), _long(31))
356 def testTestReturnFloat(self):
357 rv = self.obj.emit("test-float", 1.234)
358 self.failUnless(rv >= 1.233999 and rv <= 1.2400001, rv)
360 def testTestReturnDouble(self):
361 rv = self.obj.emit("test-double", 1.234)
362 self.assertEqual(rv, 1.234)
364 def testTestReturnString(self):
365 rv = self.obj.emit("test-string", "str")
366 self.assertEqual(rv, "str")
368 def testTestReturnObject(self):
369 rv = self.obj.emit("test-object", self)
370 self.assertEqual(rv, self)
372 if 'generic-c-marshaller' in gobject.features:
373 class TestCMarshaller(_TestCMarshaller, unittest.TestCase):
377 print('** WARNING: LIBFFI disabled, not testing')
381 class TestPyGValue(unittest.TestCase):
382 def testNoneNULLBoxedConversion(self):
383 class C(gobject.GObject):
384 __gsignals__ = dict(my_boxed_signal=(
385 gobject.SIGNAL_RUN_LAST,
386 gobject.type_from_name('GStrv'), ()))
389 obj.connect('my-boxed-signal', lambda obj: None)
391 obj.emit('my-boxed-signal')
392 assert not sys.last_type
394 if __name__ == '__main__':