Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / internet / test / test_gtk3reactor.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 GI/GTK3 reactor tests.
6 """
7
8 try:
9     from twisted.internet import gireactor, gtk3reactor
10     from gi.repository import Gtk, Gio
11 except ImportError:
12     gireactor = None
13
14 from twisted.internet.error import ReactorAlreadyRunning
15 from twisted.trial.unittest import TestCase, SkipTest
16 from twisted.internet.test.reactormixins import ReactorBuilder
17
18
19
20 class GtkApplicationRegistration(ReactorBuilder, TestCase):
21     """
22     GtkApplication and GApplication are supported by
23     L{twisted.internet.gtk3reactor} and L{twisted.internet.gireactor}.
24
25     We inherit from L{ReactorBuilder} in order to use some of its
26     reactor-running infrastructure, but don't need its test-creation
27     functionality.
28     """
29     if gireactor is None:
30         skip = "gtk3/gi not importable"
31
32
33     def runReactor(self, app, reactor):
34         """
35         Register the app, run the reactor, make sure app was activated, and
36         that reactor was running, and that reactor can be stopped.
37         """
38         if not hasattr(app, "quit"):
39             raise SkipTest("Version of PyGObject is too old.")
40
41         result = []
42         def stop():
43             result.append("stopped")
44             reactor.stop()
45         def activate(widget):
46             result.append("activated")
47             reactor.callLater(0, stop)
48         app.connect('activate', activate)
49
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.
54         app.hold()
55
56         reactor.registerGApplication(app)
57         ReactorBuilder.runReactor(self, reactor)
58         self.assertEqual(result, ["activated", "stopped"])
59
60
61     def test_gApplicationActivate(self):
62         """
63         L{Gio.Application} instances can be registered with a gireactor.
64         """
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)
70
71         self.runReactor(app, reactor)
72
73
74     def test_gtkApplicationActivate(self):
75         """
76         L{Gtk.Application} instances can be registered with a gtk3reactor.
77         """
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)
83
84         self.runReactor(app, reactor)
85
86
87     def test_portable(self):
88         """
89         L{gireactor.PortableGIReactor} doesn't support application
90         registration at this time.
91         """
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)
99
100
101     def test_noQuit(self):
102         """
103         Older versions of PyGObject lack C{Application.quit}, and so won't
104         allow registration.
105         """
106         reactor = gireactor.GIReactor(useGtk=False)
107         self.addCleanup(self.unbuildReactor, reactor)
108         # An app with no "quit" method:
109         app = object()
110         exc = self.assertRaises(RuntimeError, reactor.registerGApplication, app)
111         self.assertTrue(exc.args[0].startswith(
112                 "Application registration is not"))
113
114
115     def test_cantRegisterAfterRun(self):
116         """
117         It is not possible to register a C{Application} after the reactor has
118         already started.
119         """
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)
125
126         def tryRegister():
127             exc = self.assertRaises(ReactorAlreadyRunning,
128                                     reactor.registerGApplication, app)
129             self.assertEqual(exc.args[0],
130                              "Can't register application after reactor was started.")
131             reactor.stop()
132         reactor.callLater(0, tryRegister)
133         ReactorBuilder.runReactor(self, reactor)
134
135
136     def test_cantRegisterTwice(self):
137         """
138         It is not possible to register more than one C{Application}.
139         """
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.")