Debian Codegraph @

envisage/tests/test_application.py

raw
1 # (C) Copyright 2007-2025 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10 """ Tests for applications and plugins. """
11
12
13 # Standard library imports.
14 import os
15 import shutil
16 import unittest
17
18 from traits.api import Bool, Int, List
19
20 # Enthought library imports.
21 from traits.etsconfig.api import ETSConfig
22
23 from envisage.api import ExtensionPoint, Plugin, PluginManager
24 from envisage.tests.ets_config_patcher import ETSConfigPatcher
25
26 # Local imports.
27 from envisage.tests.event_tracker import EventTracker
28 from envisage.tests.support import PluginA, PluginB, PluginC, SimpleApplication
29
30
31 def vetoer(event):
32 """An observer that will veto an event."""
33
34 event.new.veto = True
35
36
37 class SimplePlugin(Plugin):
38 """A simple plugin."""
39
40 #### 'SimplePlugin' interface #############################################
41
42 started = Bool(False)
43 stopped = Bool(False)
44
45 ###########################################################################
46 # 'IPlugin' interface.
47 ###########################################################################
48
49 def start(self):
50 """Start the plugin."""
51
52 self.started = True
53 self.stopped = False
54
55 def stop(self):
56 """Stop the plugin."""
57
58 self.started = False
59 self.stopped = True
60
61
62 class BadPlugin(Plugin):
63 """A plugin that just causes trouble ;^)."""
64
65 ###########################################################################
66 # 'IPlugin' interface.
67 ###########################################################################
68
69 def start(self):
70 """Start the plugin."""
71
72 raise 1 / 0
73
74 def stop(self):
75 """Stop the plugin."""
76
77 raise 1 / 0
78
79
80 # PluginD and PluginE each contribute to the other's extension points, but both
81 # expect to be started before contributions are made.
82 # xref: enthought/envisage#417
83
84
85 class PluginD(Plugin):
86 """Plugin that expects to be started before contributing to
87 extension points."""
88
89 id = "D"
90 x = ExtensionPoint(List, id="d.x")
91
92 y = List(Int, contributes_to="e.x")
93
94 started = Bool(False)
95
96 def start(self):
97 self.started = True
98
99 def _y_default(self):
100 if self.started:
101 return [4, 5, 6]
102 else:
103 return []
104
105
106 class PluginE(Plugin):
107 """Another plugin that expects to be started before contributing to
108 extension points."""
109
110 id = "E"
111 x = ExtensionPoint(List, id="e.x")
112
113 y = List(Int, contributes_to="d.x")
114
115 started = Bool(False)
116
117 def start(self):
118 self.started = True
119
120 def _y_default(self):
121 if self.started:
122 return [1, 2, 3]
123 else:
124 return []
125
126
127 class ApplicationTestCase(unittest.TestCase):
128 """Tests for applications and plugins."""
129
130 def setUp(self):
131 """Prepares the test fixture before each test method is called."""
132
133 ets_config_patcher = ETSConfigPatcher()
134 ets_config_patcher.start()
135 self.addCleanup(ets_config_patcher.stop)
136
137 def test_home(self):
138 """home"""
139
140 application = SimpleApplication()
141
142 # Make sure we get the right default value.
143 self.assertEqual(ETSConfig.application_home, application.home)
144
145 # Delete the directory.
146 shutil.rmtree(application.home)
147
148 # Create a new application.
149 application = SimpleApplication()
150
151 # Make sure the directory got created.
152 self.assertTrue(os.path.exists(application.home))
153
154 # Delete the directory.
155 shutil.rmtree(application.home)
156
157 def test_no_plugins(self):
158 """no plugins"""
159
160 application = SimpleApplication()
161
162 tracker = EventTracker(
163 subscriptions=[
164 (application, "starting"),
165 (application, "started"),
166 (application, "stopping"),
167 (application, "stopped"),
168 ]
169 )
170
171 # Start the application.
172 started = application.start()
173 self.assertEqual(True, started)
174 self.assertEqual(["starting", "started"], tracker.event_names)
175
176 # Stop the application.
177 stopped = application.stop()
178 self.assertEqual(True, stopped)
179 self.assertEqual(
180 ["starting", "started", "stopping", "stopped"], tracker.event_names
181 )
182
183 def test_veto_starting(self):
184 """veto starting"""
185
186 application = SimpleApplication()
187
188 # This listener will veto the 'starting' event.
189 application.observe(vetoer, "starting")
190
191 tracker = EventTracker(
192 subscriptions=[
193 (application, "starting"),
194 (application, "started"),
195 (application, "stopping"),
196 (application, "stopped"),
197 ]
198 )
199
200 # Start the application.
201 started = application.start()
202 self.assertEqual(False, started)
203 self.assertTrue("started" not in tracker.event_names)
204
205 def test_veto_stopping(self):
206 """veto stopping"""
207
208 application = SimpleApplication()
209
210 # This listener will veto the 'stopping' event.
211 application.observe(vetoer, "stopping")
212
213 tracker = EventTracker(
214 subscriptions=[
215 (application, "starting"),
216 (application, "started"),
217 (application, "stopping"),
218 (application, "stopped"),
219 ]
220 )
221
222 # Start the application.
223 started = application.start()
224 self.assertEqual(["starting", "started"], tracker.event_names)
225 self.assertEqual(True, started)
226
227 # Stop the application.
228 stopped = application.stop()
229 self.assertEqual(False, stopped)
230 self.assertTrue("stopped" not in tracker.event_names)
231
232 def test_start_and_stop_errors(self):
233 """start and stop errors"""
234
235 simple_plugin = SimplePlugin()
236 bad_plugin = BadPlugin()
237 application = SimpleApplication(plugins=[simple_plugin, bad_plugin])
238
239 # Try to start the application - the bad plugin should barf.
240 with self.assertRaises(ZeroDivisionError):
241 application.start()
242
243 # Try to stop the application - the bad plugin should barf.
244 with self.assertRaises(ZeroDivisionError):
245 application.stop()
246
247 # Try to start a non-existent plugin.
248 with self.assertRaises(ValueError):
249 application.start_plugin(plugin_id="bogus")
250
251 # Try to stop a non-existent plugin.
252 with self.assertRaises(ValueError):
253 application.stop_plugin(plugin_id="bogus")
254
255 def test_extension_point(self):
256 """extension point"""
257
258 a = PluginA()
259 b = PluginB()
260 c = PluginC()
261
262 application = SimpleApplication(plugins=[a, b, c])
263 application.start()
264
265 # Make sure we can get the contributions via the application.
266 extensions = application.get_extensions("a.x")
267 self.assertEqual(6, len(extensions))
268 self.assertEqual([1, 2, 3, 98, 99, 100], extensions)
269
270 # Make sure we can get the contributions via the plugin.
271 extensions = a.x
272 self.assertEqual(6, len(extensions))
273 self.assertEqual([1, 2, 3, 98, 99, 100], extensions)
274
275 def test_extension_point_resolution_occurs_after_plugin_start(self):
276 # Regression test for enthought/envisage#417
277
278 # Given
279 d = PluginD()
280 e = PluginE()
281 application = SimpleApplication(plugins=[d, e])
282
283 # When
284 application.start()
285
286 # Then
287 self.assertEqual(
288 application.get_extensions("d.x"),
289 [1, 2, 3],
290 )
291 self.assertEqual(
292 application.get_extensions("e.x"),
293 [4, 5, 6],
294 )
295
296 def test_add_extension_point_listener(self):
297 """add extension point listener"""
298
299 a = PluginA()
300 b = PluginB()
301 c = PluginC()
302
303 # Start off with just two of the plugins.
304 application = SimpleApplication(plugins=[a, b])
305 application.start()
306
307 def listener(extension_registry, event):
308 """An extension point listener."""
309
310 listener.extension_point_id = event.extension_point_id
311 listener.added = event.added
312 listener.removed = event.removed
313
314 # Make sure we can get the contributions via the application.
315 extensions = application.get_extensions("a.x")
316 self.assertEqual(3, len(extensions))
317 self.assertEqual([1, 2, 3], extensions)
318
319 # Add the listener.
320 application.add_extension_point_listener(listener, "a.x")
321
322 # Now add the other plugin.
323 application.add_plugin(c)
324
325 # Make sure the listener was called.
326 self.assertEqual("a.x", listener.extension_point_id)
327 self.assertEqual([], listener.removed)
328 self.assertEqual([98, 99, 100], listener.added)
329
330 def test_remove_extension_point_listener(self):
331 """remove extension point listener"""
332
333 a = PluginA()
334 b = PluginB()
335 c = PluginC()
336
337 # Start off with just one of the plugins.
338 application = SimpleApplication(plugins=[a])
339 application.start()
340
341 def listener(extension_registry, event):
342 """An extension point listener."""
343
344 listener.extension_point_id = event.extension_point_id
345 listener.added = event.added
346 listener.removed = event.removed
347
348 # Make sure we can get the contributions via the application.
349 extensions = application.get_extensions("a.x")
350 self.assertEqual(0, len(extensions))
351
352 # Add the listener.
353 application.add_extension_point_listener(listener, "a.x")
354
355 # Now add one of the other plugins.
356 application.add_plugin(b)
357
358 # Make sure the listener was called.
359 self.assertEqual("a.x", listener.extension_point_id)
360 self.assertEqual([], listener.removed)
361 self.assertEqual([1, 2, 3], listener.added)
362
363 # Now remove the listener.
364 listener.extension_point_id = None
365 application.remove_extension_point_listener(listener, "a.x")
366
367 # Now add the final plugin.
368 application.add_plugin(c)
369
370 # Make sure the listener was *not* called.
371 self.assertEqual(None, listener.extension_point_id)
372
373 def test_add_plugin(self):
374 """add plugin"""
375
376 a = PluginA()
377 b = PluginB()
378 c = PluginC()
379
380 # Start off with just two of the plugins.
381 application = SimpleApplication(plugins=[a, b])
382 application.start()
383
384 # Make sure we can get the contributions via the application.
385 extensions = application.get_extensions("a.x")
386 self.assertEqual(3, len(extensions))
387 self.assertEqual([1, 2, 3], extensions)
388
389 # Make sure we can get the contributions via the plugin.
390 extensions = a.x
391 self.assertEqual(3, len(extensions))
392 self.assertEqual([1, 2, 3], extensions)
393
394 # Now add the other plugin.
395 application.add_plugin(c)
396
397 # Make sure we can get the contributions via the application.
398 extensions = application.get_extensions("a.x")
399 self.assertEqual(6, len(extensions))
400 self.assertEqual([1, 2, 3, 98, 99, 100], extensions)
401
402 # Make sure we can get the contributions via the plugin.
403 extensions = a.x
404 self.assertEqual(6, len(extensions))
405 self.assertEqual([1, 2, 3, 98, 99, 100], extensions)
406
407 def test_get_plugin(self):
408 """get plugin"""
409
410 a = PluginA()
411 b = PluginB()
412 c = PluginC()
413
414 # Start off with just two of the plugins.
415 application = SimpleApplication(plugins=[a, b, c])
416 application.start()
417
418 # Make sure we can get the plugins.
419 self.assertEqual(a, application.get_plugin("A"))
420 self.assertEqual(b, application.get_plugin("B"))
421 self.assertEqual(c, application.get_plugin("C"))
422
423 # Make sure we can't get one that isn't there ;^)
424 self.assertEqual(None, application.get_plugin("BOGUS"))
425
426 def test_remove_plugin(self):
427 """remove plugin"""
428
429 a = PluginA()
430 b = PluginB()
431 c = PluginC()
432
433 application = SimpleApplication(plugins=[a, b, c])
434 application.start()
435
436 # Make sure we can get the contributions via the application.
437 extensions = application.get_extensions("a.x")
438 self.assertEqual(6, len(extensions))
439 self.assertEqual([1, 2, 3, 98, 99, 100], extensions)
440
441 # Make sure we can get the contributions via the plugin.
442 extensions = a.x
443 self.assertEqual(6, len(extensions))
444 self.assertEqual([1, 2, 3, 98, 99, 100], extensions)
445
446 # Now remove one plugin.
447 application.remove_plugin(b)
448
449 # Make sure we can get the contributions via the application.
450 extensions = application.get_extensions("a.x")
451 self.assertEqual(3, len(extensions))
452 self.assertEqual([98, 99, 100], extensions)
453
454 # Make sure we can get the contributions via the plugin.
455 extensions = a.x
456 self.assertEqual(3, len(extensions))
457 self.assertEqual([98, 99, 100], extensions)
458
459 def test_set_plugin_manager_at_contruction_time(self):
460 """set plugin manager at construction time"""
461
462 a = PluginA()
463 b = PluginB()
464 c = PluginC()
465
466 # Start off with just two of the plugins.
467 application = SimpleApplication(
468 plugin_manager=PluginManager(plugins=[a, b, c])
469 )
470 application.start()
471
472 # Make sure we can get the plugins.
473 self.assertEqual(a, application.get_plugin("A"))
474 self.assertEqual(b, application.get_plugin("B"))
475 self.assertEqual(c, application.get_plugin("C"))
476
477 # Make sure we can't get one that isn't there ;^)
478 self.assertEqual(None, application.get_plugin("BOGUS"))