1# -*- Mode: Python -*- 2 3import unittest 4 5import glib 6 7 8class Idle(glib.Idle): 9 def __init__(self, loop): 10 glib.Idle.__init__(self) 11 self.count = 0 12 self.set_callback(self.callback, loop) 13 14 def callback(self, loop): 15 self.count += 1 16 return True 17 18 19class MySource(glib.Source): 20 def __init__(self): 21 glib.Source.__init__(self) 22 23 def prepare(self): 24 return True, 0 25 26 def check(self): 27 return True 28 29 def dispatch(self, callback, args): 30 return callback(*args) 31 32 33class TestSource(unittest.TestCase): 34 def timeout_callback(self, loop): 35 loop.quit() 36 37 def my_callback(self, loop): 38 self.pos += 1 39 return True 40 41 def setup_timeout(self, loop): 42 timeout = glib.Timeout(500) 43 timeout.set_callback(self.timeout_callback, loop) 44 timeout.attach() 45 46 def testSources(self): 47 loop = glib.MainLoop() 48 49 self.setup_timeout(loop) 50 51 idle = Idle(loop) 52 idle.attach() 53 54 self.pos = 0 55 56 m = MySource() 57 m.set_callback(self.my_callback, loop) 58 m.attach() 59 60 loop.run() 61 62 assert self.pos >= 0 and idle.count >= 0 63 64 def testSourcePrepare(self): 65 # this test may not terminate if prepare() is wrapped incorrectly 66 dispatched = [False] 67 loop = glib.MainLoop() 68 69 class CustomTimeout(glib.Source): 70 def prepare(self): 71 return (False, 10) 72 73 def check(self): 74 return True 75 76 def dispatch(self, callback, args): 77 dispatched[0] = True 78 79 loop.quit() 80 81 return False 82 83 source = CustomTimeout() 84 85 source.attach() 86 source.set_callback(dir) 87 88 loop.run() 89 90 assert dispatched[0] 91 92 93class TestTimeout(unittest.TestCase): 94 def test504337(self): 95 timeout_source = glib.Timeout(20) 96 idle_source = glib.Idle() 97 98 99if __name__ == '__main__': 100 unittest.main() 101