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