6 from gi.repository import GLib, GObject
7 from gi import PyGIDeprecationWarning
10 class Idle(GLib.Idle):
11 def __init__(self, loop):
12 GLib.Idle.__init__(self)
14 self.set_callback(self.callback, loop)
16 def callback(self, loop):
21 class MySource(GLib.Source):
23 GLib.Source.__init__(self)
31 def dispatch(self, callback, args):
32 return callback(*args)
35 class TestSource(unittest.TestCase):
36 def timeout_callback(self, loop):
39 def my_callback(self, loop):
43 def setup_timeout(self, loop):
44 timeout = GLib.Timeout(500)
45 timeout.set_callback(self.timeout_callback, loop)
48 def test_sources(self):
49 loop = GLib.MainLoop()
51 self.setup_timeout(loop)
54 self.assertEqual(idle.get_context(), None)
56 self.assertEqual(idle.get_context(), GLib.main_context_default())
61 self.assertEqual(m.get_context(), None)
62 m.set_callback(self.my_callback, loop)
64 self.assertEqual(m.get_context(), GLib.main_context_default())
71 self.assertGreater(self.pos, 0)
72 self.assertGreaterEqual(idle.count, 0)
73 self.assertTrue(m.is_destroyed())
74 self.assertTrue(idle.is_destroyed())
76 def test_source_prepare(self):
77 # this test may not terminate if prepare() is wrapped incorrectly
79 loop = GLib.MainLoop()
81 class CustomTimeout(GLib.Source):
88 def dispatch(self, callback, args):
95 source = CustomTimeout()
98 source.set_callback(dir)
104 def test_is_destroyed_simple(self):
107 self.assertFalse(s.is_destroyed())
109 self.assertTrue(s.is_destroyed())
111 c = GLib.MainContext()
114 self.assertFalse(s.is_destroyed())
116 self.assertTrue(s.is_destroyed())
118 def test_is_destroyed_context(self):
120 c = GLib.MainContext()
126 self.assertTrue(s.is_destroyed())
128 def test_remove(self):
129 s = GLib.idle_add(dir)
130 self.assertEqual(GLib.source_remove(s), True)
131 # s is now removed, should fail now
132 self.assertEqual(GLib.source_remove(s), False)
134 # accepts large source IDs (they are unsigned)
135 self.assertEqual(GLib.source_remove(GObject.G_MAXINT32), False)
136 self.assertEqual(GLib.source_remove(GObject.G_MAXINT32 + 1), False)
137 self.assertEqual(GLib.source_remove(GObject.G_MAXUINT32), False)
139 def test_recurse_property(self):
141 self.assertTrue(s.can_recurse in [False, True])
142 s.can_recurse = False
143 self.assertFalse(s.can_recurse)
145 def test_priority(self):
147 self.assertEqual(s.priority, GLib.PRIORITY_DEFAULT_IDLE)
148 s.priority = GLib.PRIORITY_HIGH
149 self.assertEqual(s.priority, GLib.PRIORITY_HIGH)
151 s = GLib.Idle(GLib.PRIORITY_LOW)
152 self.assertEqual(s.priority, GLib.PRIORITY_LOW)
154 s = GLib.Timeout(1, GLib.PRIORITY_LOW)
155 self.assertEqual(s.priority, GLib.PRIORITY_LOW)
158 self.assertEqual(s.priority, GLib.PRIORITY_DEFAULT)
160 def test_get_current_time(self):
161 # Note, deprecated API
163 with warnings.catch_warnings(record=True) as w:
164 warnings.simplefilter('always')
165 time = s.get_current_time()
166 self.assertTrue(issubclass(w[0].category, PyGIDeprecationWarning))
168 self.assertTrue(isinstance(time, float))
169 # plausibility check, and check magnitude of result
170 self.assertGreater(time, 1300000000.0)
171 self.assertLess(time, 2000000000.0)
173 def test_add_remove_poll(self):
174 # FIXME: very shallow test, only verifies the API signature
175 pollfd = GLib.PollFD(99, GLib.IOCondition.IN | GLib.IOCondition.HUP)
176 self.assertEqual(pollfd.fd, 99)
177 source = GLib.Source()
178 source.add_poll(pollfd)
179 source.remove_poll(pollfd)
181 def test_out_of_scope_before_dispatch(self):
182 # https://bugzilla.gnome.org/show_bug.cgi?id=504337
186 def test_finalize(self):
187 self.dispatched = False
188 self.finalized = False
190 class S(GLib.Source):
194 def dispatch(s, callback, args):
195 self.dispatched = True
199 self.finalized = True
203 print('source id:', id)
204 self.assertFalse(self.finalized)
205 self.assertFalse(source.is_destroyed())
207 while source.get_context().iteration(False):
211 self.assertTrue(self.dispatched)
212 self.assertFalse(self.finalized)
213 self.assertTrue(source.is_destroyed())
215 self.assertTrue(self.finalized)
218 class TestUserData(unittest.TestCase):
219 def test_idle_no_data(self):
224 id = GLib.idle_add(cb)
225 self.assertEqual(ml.get_context().find_source_by_id(id).priority,
226 GLib.PRIORITY_DEFAULT_IDLE)
229 def test_timeout_no_data(self):
234 id = GLib.timeout_add(50, cb)
235 self.assertEqual(ml.get_context().find_source_by_id(id).priority,
236 GLib.PRIORITY_DEFAULT)
239 def test_idle_data(self):
243 data['called'] = True
246 id = GLib.idle_add(cb, data)
247 self.assertEqual(ml.get_context().find_source_by_id(id).priority,
248 GLib.PRIORITY_DEFAULT_IDLE)
250 self.assertTrue(data['called'])
252 def test_idle_multidata(self):
256 data['called'] = True
257 data['data2'] = data2
260 id = GLib.idle_add(cb, data, 'hello')
261 self.assertEqual(ml.get_context().find_source_by_id(id).priority,
262 GLib.PRIORITY_DEFAULT_IDLE)
264 self.assertTrue(data['called'])
265 self.assertEqual(data['data2'], 'hello')
267 def test_timeout_data(self):
271 data['called'] = True
274 id = GLib.timeout_add(50, cb, data)
275 self.assertEqual(ml.get_context().find_source_by_id(id).priority,
276 GLib.PRIORITY_DEFAULT)
278 self.assertTrue(data['called'])
280 def test_timeout_multidata(self):
284 data['called'] = True
285 data['data2'] = data2
288 id = GLib.timeout_add(50, cb, data, 'hello')
289 self.assertEqual(ml.get_context().find_source_by_id(id).priority,
290 GLib.PRIORITY_DEFAULT)
292 self.assertTrue(data['called'])
293 self.assertEqual(data['data2'], 'hello')
295 def test_idle_no_data_priority(self):
300 id = GLib.idle_add(cb, priority=GLib.PRIORITY_HIGH)
301 self.assertEqual(ml.get_context().find_source_by_id(id).priority,
305 def test_timeout_no_data_priority(self):
310 id = GLib.timeout_add(50, cb, priority=GLib.PRIORITY_HIGH)
311 self.assertEqual(ml.get_context().find_source_by_id(id).priority,
315 def test_idle_data_priority(self):
319 data['called'] = True
322 id = GLib.idle_add(cb, data, priority=GLib.PRIORITY_HIGH)
323 self.assertEqual(ml.get_context().find_source_by_id(id).priority,
326 self.assertTrue(data['called'])
328 def test_timeout_data_priority(self):
332 data['called'] = True
335 id = GLib.timeout_add(50, cb, data, priority=GLib.PRIORITY_HIGH)
336 self.assertEqual(ml.get_context().find_source_by_id(id).priority,
339 self.assertTrue(data['called'])
341 def cb_no_data(self):
344 def test_idle_method_callback_no_data(self):
345 self.loop = GLib.MainLoop()
346 GLib.idle_add(self.cb_no_data)
349 def cb_with_data(self, data):
350 data['called'] = True
353 def test_idle_method_callback_with_data(self):
354 self.loop = GLib.MainLoop()
356 GLib.idle_add(self.cb_with_data, data)
358 self.assertTrue(data['called'])
361 if __name__ == '__main__':