1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
9 from twisted.internet import gireactor, gtk3reactor
10 from gi.repository import Gtk, Gio
14 from twisted.internet.error import ReactorAlreadyRunning
15 from twisted.trial.unittest import TestCase, SkipTest
16 from twisted.internet.test.reactormixins import ReactorBuilder
20 class GtkApplicationRegistration(ReactorBuilder, TestCase):
22 GtkApplication and GApplication are supported by
23 L{twisted.internet.gtk3reactor} and L{twisted.internet.gireactor}.
25 We inherit from L{ReactorBuilder} in order to use some of its
26 reactor-running infrastructure, but don't need its test-creation
30 skip = "gtk3/gi not importable"
33 def runReactor(self, app, reactor):
35 Register the app, run the reactor, make sure app was activated, and
36 that reactor was running, and that reactor can be stopped.
38 if not hasattr(app, "quit"):
39 raise SkipTest("Version of PyGObject is too old.")
43 result.append("stopped")
46 result.append("activated")
47 reactor.callLater(0, stop)
48 app.connect('activate', activate)
50 # We want reactor.stop() to *always* stop the event loop, even if
51 # someone has called hold() on the application and never done the
52 # corresponding release() -- for more details see
53 # http://developer.gnome.org/gio/unstable/GApplication.html.
56 reactor.registerGApplication(app)
57 ReactorBuilder.runReactor(self, reactor)
58 self.assertEqual(result, ["activated", "stopped"])
61 def test_gApplicationActivate(self):
63 L{Gio.Application} instances can be registered with a gireactor.
65 reactor = gireactor.GIReactor(useGtk=False)
66 self.addCleanup(self.unbuildReactor, reactor)
67 app = Gio.Application(
68 application_id='com.twistedmatrix.trial.gireactor',
69 flags=Gio.ApplicationFlags.FLAGS_NONE)
71 self.runReactor(app, reactor)
74 def test_gtkApplicationActivate(self):
76 L{Gtk.Application} instances can be registered with a gtk3reactor.
78 reactor = gtk3reactor.Gtk3Reactor()
79 self.addCleanup(self.unbuildReactor, reactor)
80 app = Gtk.Application(
81 application_id='com.twistedmatrix.trial.gtk3reactor',
82 flags=Gio.ApplicationFlags.FLAGS_NONE)
84 self.runReactor(app, reactor)
87 def test_portable(self):
89 L{gireactor.PortableGIReactor} doesn't support application
90 registration at this time.
92 reactor = gireactor.PortableGIReactor()
93 self.addCleanup(self.unbuildReactor, reactor)
94 app = Gio.Application(
95 application_id='com.twistedmatrix.trial.gireactor',
96 flags=Gio.ApplicationFlags.FLAGS_NONE)
97 self.assertRaises(NotImplementedError,
98 reactor.registerGApplication, app)
101 def test_noQuit(self):
103 Older versions of PyGObject lack C{Application.quit}, and so won't
106 reactor = gireactor.GIReactor(useGtk=False)
107 self.addCleanup(self.unbuildReactor, reactor)
108 # An app with no "quit" method:
110 exc = self.assertRaises(RuntimeError, reactor.registerGApplication, app)
111 self.assertTrue(exc.args[0].startswith(
112 "Application registration is not"))
115 def test_cantRegisterAfterRun(self):
117 It is not possible to register a C{Application} after the reactor has
120 reactor = gireactor.GIReactor(useGtk=False)
121 self.addCleanup(self.unbuildReactor, reactor)
122 app = Gio.Application(
123 application_id='com.twistedmatrix.trial.gireactor',
124 flags=Gio.ApplicationFlags.FLAGS_NONE)
127 exc = self.assertRaises(ReactorAlreadyRunning,
128 reactor.registerGApplication, app)
129 self.assertEqual(exc.args[0],
130 "Can't register application after reactor was started.")
132 reactor.callLater(0, tryRegister)
133 ReactorBuilder.runReactor(self, reactor)
136 def test_cantRegisterTwice(self):
138 It is not possible to register more than one C{Application}.
140 reactor = gireactor.GIReactor(useGtk=False)
141 self.addCleanup(self.unbuildReactor, reactor)
142 app = Gio.Application(
143 application_id='com.twistedmatrix.trial.gireactor',
144 flags=Gio.ApplicationFlags.FLAGS_NONE)
145 reactor.registerGApplication(app)
146 app2 = Gio.Application(
147 application_id='com.twistedmatrix.trial.gireactor2',
148 flags=Gio.ApplicationFlags.FLAGS_NONE)
149 exc = self.assertRaises(RuntimeError,
150 reactor.registerGApplication, app2)
151 self.assertEqual(exc.args[0],
152 "Can't register more than one application instance.")