PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/test_glib.py

https://gitlab.com/gregier/pygobject
Python | 247 lines | 223 code | 18 blank | 6 comment | 3 complexity | f888f035863a3ddd700de0f1a43409a6 MD5 | raw file
  1. # -*- Mode: Python -*-
  2. # encoding: UTF-8
  3. import unittest
  4. import os.path
  5. import warnings
  6. import subprocess
  7. from gi.repository import GLib
  8. from gi import PyGIDeprecationWarning
  9. from compathelper import _unicode, _bytes
  10. class TestGLib(unittest.TestCase):
  11. def test_find_program_in_path(self):
  12. bash_path = GLib.find_program_in_path('bash')
  13. self.assertTrue(bash_path.endswith('/bash'))
  14. self.assertTrue(os.path.exists(bash_path))
  15. self.assertEqual(GLib.find_program_in_path('non existing'), None)
  16. def test_markup_escape_text(self):
  17. self.assertEqual(GLib.markup_escape_text(_unicode('a&bä')), 'a&bä')
  18. self.assertEqual(GLib.markup_escape_text(_bytes('a&b\x05')), 'a&b')
  19. # with explicit length argument
  20. self.assertEqual(GLib.markup_escape_text(_bytes('a\x05\x01\x02'), 2), 'a')
  21. def test_progname(self):
  22. GLib.set_prgname('moo')
  23. self.assertEqual(GLib.get_prgname(), 'moo')
  24. def test_appname(self):
  25. GLib.set_application_name('moo')
  26. self.assertEqual(GLib.get_application_name(), 'moo')
  27. def test_xdg_dirs(self):
  28. d = GLib.get_user_data_dir()
  29. self.assertTrue('/' in d, d)
  30. d = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_DESKTOP)
  31. self.assertTrue('/' in d, d)
  32. with warnings.catch_warnings():
  33. warnings.simplefilter('ignore', PyGIDeprecationWarning)
  34. # also works with backwards compatible enum names
  35. self.assertEqual(GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_MUSIC),
  36. GLib.get_user_special_dir(GLib.USER_DIRECTORY_MUSIC))
  37. for d in GLib.get_system_config_dirs():
  38. self.assertTrue('/' in d, d)
  39. for d in GLib.get_system_data_dirs():
  40. self.assertTrue(isinstance(d, str), d)
  41. def test_main_depth(self):
  42. self.assertEqual(GLib.main_depth(), 0)
  43. def test_filenames(self):
  44. self.assertEqual(GLib.filename_display_name('foo'), 'foo')
  45. self.assertEqual(GLib.filename_display_basename('bar/foo'), 'foo')
  46. # this is locale dependent, so we cannot completely verify the result
  47. res = GLib.filename_from_utf8(_unicode('aäb'))
  48. self.assertTrue(isinstance(res, bytes))
  49. self.assertGreaterEqual(len(res), 3)
  50. # with explicit length argument
  51. self.assertEqual(GLib.filename_from_utf8(_unicode('aäb'), 1), b'a')
  52. def test_uri_extract(self):
  53. res = GLib.uri_list_extract_uris('''# some comment
  54. http://example.com
  55. https://my.org/q?x=1&y=2
  56. http://gnome.org/new''')
  57. self.assertEqual(res, ['http://example.com',
  58. 'https://my.org/q?x=1&y=2',
  59. 'http://gnome.org/new'])
  60. def test_current_time(self):
  61. with warnings.catch_warnings(record=True) as warn:
  62. warnings.simplefilter('always')
  63. tm = GLib.get_current_time()
  64. self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
  65. self.assertTrue(isinstance(tm, float))
  66. self.assertGreater(tm, 1350000000.0)
  67. def test_main_loop(self):
  68. # note we do not test run() here, as we use this in countless other
  69. # tests
  70. ml = GLib.MainLoop()
  71. self.assertFalse(ml.is_running())
  72. context = ml.get_context()
  73. self.assertEqual(context, GLib.MainContext.default())
  74. self.assertTrue(context.is_owner() in [True, False])
  75. self.assertTrue(context.pending() in [True, False])
  76. self.assertFalse(context.iteration(False))
  77. def test_main_loop_with_context(self):
  78. context = GLib.MainContext()
  79. ml = GLib.MainLoop(context)
  80. self.assertFalse(ml.is_running())
  81. self.assertEqual(ml.get_context(), context)
  82. def test_main_context(self):
  83. # constructor
  84. context = GLib.MainContext()
  85. self.assertTrue(context.is_owner() in [True, False])
  86. self.assertFalse(context.pending())
  87. self.assertFalse(context.iteration(False))
  88. # GLib API
  89. context = GLib.MainContext.default()
  90. self.assertTrue(context.is_owner() in [True, False])
  91. self.assertTrue(context.pending() in [True, False])
  92. self.assertTrue(context.iteration(False) in [True, False])
  93. # backwards compatible API
  94. context = GLib.main_context_default()
  95. self.assertTrue(context.is_owner() in [True, False])
  96. self.assertTrue(context.pending() in [True, False])
  97. self.assertTrue(context.iteration(False) in [True, False])
  98. def test_io_add_watch_no_data(self):
  99. (r, w) = os.pipe()
  100. call_data = []
  101. def cb(fd, condition):
  102. call_data.append((fd, condition, os.read(fd, 1)))
  103. return True
  104. # io_add_watch() takes an IOChannel, calling with an fd is deprecated
  105. with warnings.catch_warnings(record=True) as warn:
  106. warnings.simplefilter('always')
  107. GLib.io_add_watch(r, GLib.IOCondition.IN, cb)
  108. self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
  109. ml = GLib.MainLoop()
  110. GLib.timeout_add(10, lambda: os.write(w, b'a') and False)
  111. GLib.timeout_add(100, lambda: os.write(w, b'b') and False)
  112. GLib.timeout_add(200, ml.quit)
  113. ml.run()
  114. self.assertEqual(call_data, [(r, GLib.IOCondition.IN, b'a'),
  115. (r, GLib.IOCondition.IN, b'b')])
  116. def test_io_add_watch_with_data(self):
  117. (r, w) = os.pipe()
  118. call_data = []
  119. def cb(fd, condition, data):
  120. call_data.append((fd, condition, os.read(fd, 1), data))
  121. return True
  122. # io_add_watch() takes an IOChannel, calling with an fd is deprecated
  123. with warnings.catch_warnings(record=True) as warn:
  124. warnings.simplefilter('always')
  125. GLib.io_add_watch(r, GLib.IOCondition.IN, cb, 'moo')
  126. self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
  127. ml = GLib.MainLoop()
  128. GLib.timeout_add(10, lambda: os.write(w, b'a') and False)
  129. GLib.timeout_add(100, lambda: os.write(w, b'b') and False)
  130. GLib.timeout_add(200, ml.quit)
  131. ml.run()
  132. self.assertEqual(call_data, [(r, GLib.IOCondition.IN, b'a', 'moo'),
  133. (r, GLib.IOCondition.IN, b'b', 'moo')])
  134. def test_io_add_watch_with_multiple_data(self):
  135. (r, w) = os.pipe()
  136. call_data = []
  137. def cb(fd, condition, *user_data):
  138. call_data.append((fd, condition, os.read(fd, 1), user_data))
  139. return True
  140. # io_add_watch() takes an IOChannel, calling with an fd is deprecated
  141. with warnings.catch_warnings(record=True) as warn:
  142. warnings.simplefilter('always')
  143. GLib.io_add_watch(r, GLib.IOCondition.IN, cb, 'moo', 'foo')
  144. self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
  145. ml = GLib.MainLoop()
  146. GLib.timeout_add(10, lambda: os.write(w, b'a') and False)
  147. GLib.timeout_add(100, ml.quit)
  148. ml.run()
  149. self.assertEqual(call_data, [(r, GLib.IOCondition.IN, b'a', ('moo', 'foo'))])
  150. def test_io_add_watch_pyfile(self):
  151. call_data = []
  152. cmd = subprocess.Popen('sleep 0.1; echo hello; sleep 0.2; echo world',
  153. shell=True, stdout=subprocess.PIPE)
  154. def cb(file, condition):
  155. call_data.append((file, condition, file.readline()))
  156. if len(call_data) == 2:
  157. # avoid having to wait for the full timeout
  158. ml.quit()
  159. return True
  160. # io_add_watch() takes an IOChannel, calling with a Python file is deprecated
  161. with warnings.catch_warnings(record=True) as warn:
  162. warnings.simplefilter('always')
  163. GLib.io_add_watch(cmd.stdout, GLib.IOCondition.IN, cb)
  164. self.assertTrue(issubclass(warn[0].category, PyGIDeprecationWarning))
  165. ml = GLib.MainLoop()
  166. GLib.timeout_add(2000, ml.quit)
  167. ml.run()
  168. cmd.wait()
  169. self.assertEqual(call_data, [(cmd.stdout, GLib.IOCondition.IN, b'hello\n'),
  170. (cmd.stdout, GLib.IOCondition.IN, b'world\n')])
  171. def test_glib_version(self):
  172. with warnings.catch_warnings():
  173. warnings.simplefilter('ignore', PyGIDeprecationWarning)
  174. (major, minor, micro) = GLib.glib_version
  175. self.assertGreaterEqual(major, 2)
  176. self.assertGreaterEqual(minor, 0)
  177. self.assertGreaterEqual(micro, 0)
  178. def test_pyglib_version(self):
  179. with warnings.catch_warnings():
  180. warnings.simplefilter('ignore', PyGIDeprecationWarning)
  181. (major, minor, micro) = GLib.pyglib_version
  182. self.assertGreaterEqual(major, 3)
  183. self.assertGreaterEqual(minor, 0)
  184. self.assertGreaterEqual(micro, 0)
  185. def test_timezone_constructor(self):
  186. timezone = GLib.TimeZone("+05:21")
  187. self.assertEqual(timezone.get_offset(0), ((5 * 60) + 21) * 60)
  188. def test_source_attach_implicit_context(self):
  189. context = GLib.MainContext.default()
  190. source = GLib.Idle()
  191. source_id = source.attach()
  192. self.assertEqual(context, source.get_context())
  193. self.assertTrue(GLib.Source.remove(source_id))