Imported Upstream version 3.25.1
[platform/upstream/pygobject2.git] / tests / test_glib.py
1 # -*- Mode: Python -*-
2 # encoding: UTF-8
3
4 import os
5 import sys
6 import unittest
7 import os.path
8 import warnings
9 import subprocess
10
11 from gi.repository import GLib
12 from gi import PyGIDeprecationWarning
13
14
15 class TestGLib(unittest.TestCase):
16
17     @unittest.skipIf(os.name == "nt", "no bash on Windows")
18     def test_find_program_in_path(self):
19         bash_path = GLib.find_program_in_path('bash')
20         self.assertTrue(bash_path.endswith(os.path.sep + 'bash'))
21         self.assertTrue(os.path.exists(bash_path))
22
23         self.assertEqual(GLib.find_program_in_path('non existing'), None)
24
25     def test_markup_escape_text(self):
26         self.assertEqual(GLib.markup_escape_text(u'a&bä'), 'a&bä')
27         self.assertEqual(GLib.markup_escape_text(b'a&b\x05'), 'a&b')
28
29         # with explicit length argument
30         self.assertEqual(GLib.markup_escape_text(b'a\x05\x01\x02', 2), 'a')
31
32     def test_progname(self):
33         GLib.set_prgname('moo')
34         self.assertEqual(GLib.get_prgname(), 'moo')
35
36     def test_appname(self):
37         GLib.set_application_name('moo')
38         self.assertEqual(GLib.get_application_name(), 'moo')
39
40     def test_xdg_dirs(self):
41         d = GLib.get_user_data_dir()
42         self.assertTrue(os.path.sep in d, d)
43         d = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_DESKTOP)
44         self.assertTrue(os.path.sep in d, d)
45         with warnings.catch_warnings():
46             warnings.simplefilter('ignore', PyGIDeprecationWarning)
47
48             # also works with backwards compatible enum names
49             self.assertEqual(GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_MUSIC),
50                              GLib.get_user_special_dir(GLib.USER_DIRECTORY_MUSIC))
51
52         for d in GLib.get_system_config_dirs():
53             self.assertTrue(os.path.sep in d, d)
54         for d in GLib.get_system_data_dirs():
55             self.assertTrue(isinstance(d, str), d)
56
57     def test_main_depth(self):
58         self.assertEqual(GLib.main_depth(), 0)
59
60     def test_filenames(self):
61         self.assertEqual(GLib.filename_display_name('foo'), 'foo')
62         self.assertEqual(GLib.filename_display_basename('bar/foo'), 'foo')
63
64         # this is locale dependent, so we cannot completely verify the result
65         res = GLib.filename_from_utf8(u'aäb')
66         self.assertTrue(isinstance(res, bytes))
67         self.assertGreaterEqual(len(res), 3)
68
69         # with explicit length argument
70         self.assertEqual(GLib.filename_from_utf8(u'aäb', 1), b'a')
71
72     def test_uri_extract(self):
73         res = GLib.uri_list_extract_uris('''# some comment
74 http://example.com
75 https://my.org/q?x=1&y=2
76             http://gnome.org/new''')
77         self.assertEqual(res, ['http://example.com',
78                                'https://my.org/q?x=1&y=2',
79                                'http://gnome.org/new'])
80
81     def test_current_time(self):
82         with warnings.catch_warnings(record=True) as warn:
83             warnings.simplefilter('always')
84             tm = GLib.get_current_time()
85             self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
86
87         self.assertTrue(isinstance(tm, float))
88         self.assertGreater(tm, 1350000000.0)
89
90     @unittest.skipIf(sys.platform == "darwin", "fails on OSX")
91     def test_main_loop(self):
92         # note we do not test run() here, as we use this in countless other
93         # tests
94         ml = GLib.MainLoop()
95         self.assertFalse(ml.is_running())
96
97         context = ml.get_context()
98         self.assertEqual(context, GLib.MainContext.default())
99         self.assertTrue(context.is_owner() in [True, False])
100         self.assertTrue(context.pending() in [True, False])
101         self.assertFalse(context.iteration(False))
102
103     def test_main_loop_with_context(self):
104         context = GLib.MainContext()
105         ml = GLib.MainLoop(context)
106         self.assertFalse(ml.is_running())
107         self.assertEqual(ml.get_context(), context)
108
109     def test_main_context(self):
110         # constructor
111         context = GLib.MainContext()
112         self.assertTrue(context.is_owner() in [True, False])
113         self.assertFalse(context.pending())
114         self.assertFalse(context.iteration(False))
115
116         # GLib API
117         context = GLib.MainContext.default()
118         self.assertTrue(context.is_owner() in [True, False])
119         self.assertTrue(context.pending() in [True, False])
120         self.assertTrue(context.iteration(False) in [True, False])
121
122         # backwards compatible API
123         context = GLib.main_context_default()
124         self.assertTrue(context.is_owner() in [True, False])
125         self.assertTrue(context.pending() in [True, False])
126         self.assertTrue(context.iteration(False) in [True, False])
127
128     @unittest.skipIf(os.name == "nt", "hangs")
129     def test_io_add_watch_no_data(self):
130         (r, w) = os.pipe()
131         call_data = []
132
133         def cb(fd, condition):
134             call_data.append((fd, condition, os.read(fd, 1)))
135             if len(call_data) == 2:
136                 ml.quit()
137             return True
138
139         # io_add_watch() takes an IOChannel, calling with an fd is deprecated
140         with warnings.catch_warnings(record=True) as warn:
141             warnings.simplefilter('always')
142             GLib.io_add_watch(r, GLib.IOCondition.IN, cb,
143                               priority=GLib.PRIORITY_HIGH)
144             self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
145
146         def write():
147             os.write(w, b'a')
148             GLib.idle_add(lambda: os.write(w, b'b') and False)
149
150         ml = GLib.MainLoop()
151         GLib.idle_add(write)
152         GLib.timeout_add(2000, ml.quit)
153         ml.run()
154
155         self.assertEqual(call_data, [(r, GLib.IOCondition.IN, b'a'),
156                                      (r, GLib.IOCondition.IN, b'b')])
157
158     @unittest.skipIf(os.name == "nt", "hangs")
159     def test_io_add_watch_with_data(self):
160         (r, w) = os.pipe()
161         call_data = []
162
163         def cb(fd, condition, data):
164             call_data.append((fd, condition, os.read(fd, 1), data))
165             if len(call_data) == 2:
166                 ml.quit()
167             return True
168
169         # io_add_watch() takes an IOChannel, calling with an fd is deprecated
170         with warnings.catch_warnings(record=True) as warn:
171             warnings.simplefilter('always')
172             GLib.io_add_watch(r, GLib.IOCondition.IN, cb, 'moo',
173                               priority=GLib.PRIORITY_HIGH)
174             self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
175
176         def write():
177             os.write(w, b'a')
178             GLib.idle_add(lambda: os.write(w, b'b') and False)
179
180         ml = GLib.MainLoop()
181         GLib.idle_add(write)
182         GLib.timeout_add(2000, ml.quit)
183         ml.run()
184
185         self.assertEqual(call_data, [(r, GLib.IOCondition.IN, b'a', 'moo'),
186                                      (r, GLib.IOCondition.IN, b'b', 'moo')])
187
188     @unittest.skipIf(os.name == "nt", "hangs")
189     def test_io_add_watch_with_multiple_data(self):
190         (r, w) = os.pipe()
191         call_data = []
192
193         def cb(fd, condition, *user_data):
194             call_data.append((fd, condition, os.read(fd, 1), user_data))
195             ml.quit()
196             return True
197
198         # io_add_watch() takes an IOChannel, calling with an fd is deprecated
199         with warnings.catch_warnings(record=True) as warn:
200             warnings.simplefilter('always')
201             GLib.io_add_watch(r, GLib.IOCondition.IN, cb, 'moo', 'foo')
202             self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
203
204         ml = GLib.MainLoop()
205         GLib.idle_add(lambda: os.write(w, b'a') and False)
206         GLib.timeout_add(2000, ml.quit)
207         ml.run()
208
209         self.assertEqual(call_data, [(r, GLib.IOCondition.IN, b'a', ('moo', 'foo'))])
210
211     @unittest.skipIf(os.name == "nt", "no shell on Windows")
212     def test_io_add_watch_pyfile(self):
213         call_data = []
214
215         cmd = subprocess.Popen('echo hello; echo world',
216                                shell=True, bufsize=0, stdout=subprocess.PIPE)
217
218         def cb(file, condition):
219             call_data.append((file, condition, file.readline()))
220             if len(call_data) == 2:
221                 # avoid having to wait for the full timeout
222                 ml.quit()
223             return True
224
225         # io_add_watch() takes an IOChannel, calling with a Python file is deprecated
226         with warnings.catch_warnings(record=True) as warn:
227             warnings.simplefilter('always')
228             GLib.io_add_watch(cmd.stdout, GLib.IOCondition.IN, cb)
229             self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
230
231         ml = GLib.MainLoop()
232         GLib.timeout_add(2000, ml.quit)
233         ml.run()
234
235         cmd.wait()
236
237         self.assertEqual(call_data, [(cmd.stdout, GLib.IOCondition.IN, b'hello\n'),
238                                      (cmd.stdout, GLib.IOCondition.IN, b'world\n')])
239
240     def test_glib_version(self):
241         with warnings.catch_warnings():
242             warnings.simplefilter('ignore', PyGIDeprecationWarning)
243
244             (major, minor, micro) = GLib.glib_version
245             self.assertGreaterEqual(major, 2)
246             self.assertGreaterEqual(minor, 0)
247             self.assertGreaterEqual(micro, 0)
248
249     def test_pyglib_version(self):
250         with warnings.catch_warnings():
251             warnings.simplefilter('ignore', PyGIDeprecationWarning)
252
253             (major, minor, micro) = GLib.pyglib_version
254             self.assertGreaterEqual(major, 3)
255             self.assertGreaterEqual(minor, 0)
256             self.assertGreaterEqual(micro, 0)
257
258     def test_timezone_constructor(self):
259         timezone = GLib.TimeZone("+05:21")
260         self.assertEqual(timezone.get_offset(0), ((5 * 60) + 21) * 60)
261
262     def test_source_attach_implicit_context(self):
263         context = GLib.MainContext.default()
264         source = GLib.Idle()
265         source_id = source.attach()
266         self.assertEqual(context, source.get_context())
267         self.assertTrue(GLib.Source.remove(source_id))