test_gireactor.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. GI/GTK3 reactor tests.
  5. """
  6. from __future__ import division, absolute_import, print_function
  7. import sys, os
  8. try:
  9. from twisted.internet import gireactor
  10. from gi.repository import Gio
  11. except ImportError:
  12. gireactor = None
  13. gtk3reactor = None
  14. else:
  15. # gtk3reactor may be unavailable even if gireactor is available; in
  16. # particular in pygobject 3.4/gtk 3.6, when no X11 DISPLAY is found.
  17. try:
  18. from twisted.internet import gtk3reactor
  19. except ImportError:
  20. gtk3reactor = None
  21. else:
  22. from gi.repository import Gtk
  23. from twisted.python.filepath import FilePath
  24. from twisted.python.runtime import platform
  25. from twisted.internet.defer import Deferred
  26. from twisted.internet.error import ReactorAlreadyRunning
  27. from twisted.internet.protocol import ProcessProtocol
  28. from twisted.trial.unittest import TestCase, SkipTest
  29. from twisted.internet.test.reactormixins import ReactorBuilder
  30. from twisted.test.test_twisted import SetAsideModule
  31. from twisted.internet.interfaces import IReactorProcess
  32. from twisted.python.compat import _PY3
  33. # Skip all tests if gi is unavailable:
  34. if gireactor is None:
  35. skip = "gtk3/gi not importable"
  36. class GApplicationRegistrationTests(ReactorBuilder, TestCase):
  37. """
  38. GtkApplication and GApplication are supported by
  39. L{twisted.internet.gtk3reactor} and L{twisted.internet.gireactor}.
  40. We inherit from L{ReactorBuilder} in order to use some of its
  41. reactor-running infrastructure, but don't need its test-creation
  42. functionality.
  43. """
  44. def runReactor(self, app, reactor):
  45. """
  46. Register the app, run the reactor, make sure app was activated, and
  47. that reactor was running, and that reactor can be stopped.
  48. """
  49. if not hasattr(app, "quit"):
  50. raise SkipTest("Version of PyGObject is too old.")
  51. result = []
  52. def stop():
  53. result.append("stopped")
  54. reactor.stop()
  55. def activate(widget):
  56. result.append("activated")
  57. reactor.callLater(0, stop)
  58. app.connect('activate', activate)
  59. # We want reactor.stop() to *always* stop the event loop, even if
  60. # someone has called hold() on the application and never done the
  61. # corresponding release() -- for more details see
  62. # http://developer.gnome.org/gio/unstable/GApplication.html.
  63. app.hold()
  64. reactor.registerGApplication(app)
  65. ReactorBuilder.runReactor(self, reactor)
  66. self.assertEqual(result, ["activated", "stopped"])
  67. def test_gApplicationActivate(self):
  68. """
  69. L{Gio.Application} instances can be registered with a gireactor.
  70. """
  71. reactor = gireactor.GIReactor(useGtk=False)
  72. self.addCleanup(self.unbuildReactor, reactor)
  73. app = Gio.Application(
  74. application_id='com.twistedmatrix.trial.gireactor',
  75. flags=Gio.ApplicationFlags.FLAGS_NONE)
  76. self.runReactor(app, reactor)
  77. def test_gtkApplicationActivate(self):
  78. """
  79. L{Gtk.Application} instances can be registered with a gtk3reactor.
  80. """
  81. reactor = gtk3reactor.Gtk3Reactor()
  82. self.addCleanup(self.unbuildReactor, reactor)
  83. app = Gtk.Application(
  84. application_id='com.twistedmatrix.trial.gtk3reactor',
  85. flags=Gio.ApplicationFlags.FLAGS_NONE)
  86. self.runReactor(app, reactor)
  87. if gtk3reactor is None:
  88. test_gtkApplicationActivate.skip = (
  89. "Gtk unavailable (may require running with X11 DISPLAY env set)")
  90. def test_portable(self):
  91. """
  92. L{gireactor.PortableGIReactor} doesn't support application
  93. registration at this time.
  94. """
  95. reactor = gireactor.PortableGIReactor()
  96. self.addCleanup(self.unbuildReactor, reactor)
  97. app = Gio.Application(
  98. application_id='com.twistedmatrix.trial.gireactor',
  99. flags=Gio.ApplicationFlags.FLAGS_NONE)
  100. self.assertRaises(NotImplementedError,
  101. reactor.registerGApplication, app)
  102. def test_noQuit(self):
  103. """
  104. Older versions of PyGObject lack C{Application.quit}, and so won't
  105. allow registration.
  106. """
  107. reactor = gireactor.GIReactor(useGtk=False)
  108. self.addCleanup(self.unbuildReactor, reactor)
  109. # An app with no "quit" method:
  110. app = object()
  111. exc = self.assertRaises(RuntimeError, reactor.registerGApplication, app)
  112. self.assertTrue(exc.args[0].startswith(
  113. "Application registration is not"))
  114. def test_cantRegisterAfterRun(self):
  115. """
  116. It is not possible to register a C{Application} after the reactor has
  117. already started.
  118. """
  119. reactor = gireactor.GIReactor(useGtk=False)
  120. self.addCleanup(self.unbuildReactor, reactor)
  121. app = Gio.Application(
  122. application_id='com.twistedmatrix.trial.gireactor',
  123. flags=Gio.ApplicationFlags.FLAGS_NONE)
  124. def tryRegister():
  125. exc = self.assertRaises(ReactorAlreadyRunning,
  126. reactor.registerGApplication, app)
  127. self.assertEqual(exc.args[0],
  128. "Can't register application after reactor was started.")
  129. reactor.stop()
  130. reactor.callLater(0, tryRegister)
  131. ReactorBuilder.runReactor(self, reactor)
  132. def test_cantRegisterTwice(self):
  133. """
  134. It is not possible to register more than one C{Application}.
  135. """
  136. reactor = gireactor.GIReactor(useGtk=False)
  137. self.addCleanup(self.unbuildReactor, reactor)
  138. app = Gio.Application(
  139. application_id='com.twistedmatrix.trial.gireactor',
  140. flags=Gio.ApplicationFlags.FLAGS_NONE)
  141. reactor.registerGApplication(app)
  142. app2 = Gio.Application(
  143. application_id='com.twistedmatrix.trial.gireactor2',
  144. flags=Gio.ApplicationFlags.FLAGS_NONE)
  145. exc = self.assertRaises(RuntimeError,
  146. reactor.registerGApplication, app2)
  147. self.assertEqual(exc.args[0],
  148. "Can't register more than one application instance.")
  149. class PygtkCompatibilityTests(TestCase):
  150. """
  151. pygtk imports are either prevented, or a compatibility layer is used if
  152. possible.
  153. """
  154. def test_noCompatibilityLayer(self):
  155. """
  156. If no compatibility layer is present, imports of gobject and friends
  157. are disallowed.
  158. We do this by running a process where we make sure gi.pygtkcompat
  159. isn't present.
  160. """
  161. if _PY3:
  162. raise SkipTest("Python3 always has the compatibility layer.")
  163. from twisted.internet import reactor
  164. if not IReactorProcess.providedBy(reactor):
  165. raise SkipTest("No process support available in this reactor.")
  166. result = Deferred()
  167. class Stdout(ProcessProtocol):
  168. data = b""
  169. def errReceived(self, err):
  170. print(err)
  171. def outReceived(self, data):
  172. self.data += data
  173. def processExited(self, reason):
  174. result.callback(self.data)
  175. path = FilePath(__file__).sibling(b"process_gireactornocompat.py").path
  176. pyExe = FilePath(sys.executable)._asBytesPath()
  177. # Pass in a PYTHONPATH that is the test runner's os.path, to make sure
  178. # we're running from a checkout
  179. reactor.spawnProcess(Stdout(), pyExe, [pyExe, path],
  180. env={"PYTHONPATH": ":".join(sys.path)})
  181. result.addCallback(self.assertEqual, b"success")
  182. return result
  183. def test_compatibilityLayer(self):
  184. """
  185. If compatibility layer is present, importing gobject uses the gi
  186. compatibility layer.
  187. """
  188. if "gi.pygtkcompat" not in sys.modules:
  189. raise SkipTest("This version of gi doesn't include pygtkcompat.")
  190. import gobject
  191. self.assertTrue(gobject.__name__.startswith("gi."))
  192. class Gtk3ReactorTests(TestCase):
  193. """
  194. Tests for L{gtk3reactor}.
  195. """
  196. def test_requiresDISPLAY(self):
  197. """
  198. On X11, L{gtk3reactor} is unimportable if the C{DISPLAY} environment
  199. variable is not set.
  200. """
  201. display = os.environ.get("DISPLAY", None)
  202. if display is not None:
  203. self.addCleanup(os.environ.__setitem__, "DISPLAY", display)
  204. del os.environ["DISPLAY"]
  205. with SetAsideModule("twisted.internet.gtk3reactor"):
  206. exc = self.assertRaises(ImportError,
  207. __import__, "twisted.internet.gtk3reactor")
  208. self.assertEqual(
  209. exc.args[0],
  210. "Gtk3 requires X11, and no DISPLAY environment variable is set")
  211. if platform.getType() != "posix" or platform.isMacOSX():
  212. test_requiresDISPLAY.skip = "This test is only relevant when using X11"