fe674cd0cf69a9be2fc60cfbb7bf152103ee648c
[platform/upstream/python-gobject.git] / tests / test_source.py
1 # -*- Mode: Python -*-
2
3 import unittest
4
5 from gi.repository import GLib
6
7
8 class 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
19 class 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
33 class 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     def testIsDestroyedSimple(self):
93         s = GLib.Source()
94
95         self.assertFalse(s.is_destroyed())
96         s.destroy()
97         self.assertTrue(s.is_destroyed())
98
99         c = GLib.MainContext()
100         s = GLib.Source()
101         s.attach(c)
102         self.assertFalse(s.is_destroyed())
103         s.destroy()
104         self.assertTrue(s.is_destroyed())
105
106     def testIsDestroyedContext(self):
107         def f():
108             c = GLib.MainContext()
109             s = GLib.Source()
110             s.attach(c)
111             return s
112
113         s = f()
114         self.assertTrue(s.is_destroyed())
115
116
117 class TestTimeout(unittest.TestCase):
118     def test504337(self):
119         GLib.Timeout(20)
120         GLib.Idle()
121
122
123 if __name__ == '__main__':
124     unittest.main()