11 from gi.repository import GLib
12 from gi import PyGIDeprecationWarning
15 class TestGLib(unittest.TestCase):
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))
23 self.assertEqual(GLib.find_program_in_path('non existing'), None)
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')
29 # with explicit length argument
30 self.assertEqual(GLib.markup_escape_text(b'a\x05\x01\x02', 2), 'a')
32 def test_progname(self):
33 GLib.set_prgname('moo')
34 self.assertEqual(GLib.get_prgname(), 'moo')
36 def test_appname(self):
37 GLib.set_application_name('moo')
38 self.assertEqual(GLib.get_application_name(), 'moo')
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)
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))
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)
57 def test_main_depth(self):
58 self.assertEqual(GLib.main_depth(), 0)
60 def test_filenames(self):
61 self.assertEqual(GLib.filename_display_name('foo'), 'foo')
62 self.assertEqual(GLib.filename_display_basename('bar/foo'), 'foo')
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)
69 # with explicit length argument
70 self.assertEqual(GLib.filename_from_utf8(u'aäb', 1), b'a')
72 def test_uri_extract(self):
73 res = GLib.uri_list_extract_uris('''# some comment
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'])
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))
87 self.assertTrue(isinstance(tm, float))
88 self.assertGreater(tm, 1350000000.0)
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
95 self.assertFalse(ml.is_running())
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))
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)
109 def test_main_context(self):
111 context = GLib.MainContext()
112 self.assertTrue(context.is_owner() in [True, False])
113 self.assertFalse(context.pending())
114 self.assertFalse(context.iteration(False))
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])
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])
128 @unittest.skipIf(os.name == "nt", "hangs")
129 def test_io_add_watch_no_data(self):
133 def cb(fd, condition):
134 call_data.append((fd, condition, os.read(fd, 1)))
135 if len(call_data) == 2:
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))
148 GLib.idle_add(lambda: os.write(w, b'b') and False)
152 GLib.timeout_add(2000, ml.quit)
155 self.assertEqual(call_data, [(r, GLib.IOCondition.IN, b'a'),
156 (r, GLib.IOCondition.IN, b'b')])
158 @unittest.skipIf(os.name == "nt", "hangs")
159 def test_io_add_watch_with_data(self):
163 def cb(fd, condition, data):
164 call_data.append((fd, condition, os.read(fd, 1), data))
165 if len(call_data) == 2:
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))
178 GLib.idle_add(lambda: os.write(w, b'b') and False)
182 GLib.timeout_add(2000, ml.quit)
185 self.assertEqual(call_data, [(r, GLib.IOCondition.IN, b'a', 'moo'),
186 (r, GLib.IOCondition.IN, b'b', 'moo')])
188 @unittest.skipIf(os.name == "nt", "hangs")
189 def test_io_add_watch_with_multiple_data(self):
193 def cb(fd, condition, *user_data):
194 call_data.append((fd, condition, os.read(fd, 1), user_data))
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))
205 GLib.idle_add(lambda: os.write(w, b'a') and False)
206 GLib.timeout_add(2000, ml.quit)
209 self.assertEqual(call_data, [(r, GLib.IOCondition.IN, b'a', ('moo', 'foo'))])
211 @unittest.skipIf(os.name == "nt", "no shell on Windows")
212 def test_io_add_watch_pyfile(self):
215 cmd = subprocess.Popen('echo hello; echo world',
216 shell=True, bufsize=0, stdout=subprocess.PIPE)
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
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))
232 GLib.timeout_add(2000, ml.quit)
237 self.assertEqual(call_data, [(cmd.stdout, GLib.IOCondition.IN, b'hello\n'),
238 (cmd.stdout, GLib.IOCondition.IN, b'world\n')])
240 def test_glib_version(self):
241 with warnings.catch_warnings():
242 warnings.simplefilter('ignore', PyGIDeprecationWarning)
244 (major, minor, micro) = GLib.glib_version
245 self.assertGreaterEqual(major, 2)
246 self.assertGreaterEqual(minor, 0)
247 self.assertGreaterEqual(micro, 0)
249 def test_pyglib_version(self):
250 with warnings.catch_warnings():
251 warnings.simplefilter('ignore', PyGIDeprecationWarning)
253 (major, minor, micro) = GLib.pyglib_version
254 self.assertGreaterEqual(major, 3)
255 self.assertGreaterEqual(minor, 0)
256 self.assertGreaterEqual(micro, 0)
258 def test_timezone_constructor(self):
259 timezone = GLib.TimeZone("+05:21")
260 self.assertEqual(timezone.get_offset(0), ((5 * 60) + 21) * 60)
262 def test_source_attach_implicit_context(self):
263 context = GLib.MainContext.default()
265 source_id = source.attach()
266 self.assertEqual(context, source.get_context())
267 self.assertTrue(GLib.Source.remove(source_id))