Debian Codegraph @

envisage/tests/test_composite_plugin_manager.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 the composite plugin manager. """
11
12 import unittest
13
14 from traits.api import Bool
15
16 from envisage.application import Application
17 from envisage.composite_plugin_manager import CompositePluginManager
18 from envisage.plugin import Plugin
19 from envisage.plugin_manager import PluginManager
20
21
22 class SimplePlugin(Plugin):
23 """A simple plugin."""
24
25 #### 'SimplePlugin' protocol ##############################################
26
27 started = Bool(False)
28 stopped = Bool(False)
29
30 #### 'IPlugin' protocol ###################################################
31
32 def start(self):
33 """Start the plugin."""
34
35 self.started = True
36 self.stopped = False
37
38 def stop(self):
39 """Stop the plugin."""
40
41 self.started = False
42 self.stopped = True
43
44
45 class CustomException(Exception):
46 """Custom exception used for testing purposes."""
47
48 pass
49
50
51 class RaisingPluginManager(PluginManager):
52 """A PluginManager that raises on iteration."""
53
54 def __iter__(self):
55 raise CustomException("Something went wrong.")
56
57
58 class CompositePluginManagerTestCase(unittest.TestCase):
59 """Tests for the composite plugin manager."""
60
61 def test_find_no_plugins_if_there_are_no_plugin_managers(self):
62 plugin_manager = CompositePluginManager()
63 ids = [plugin.id for plugin in plugin_manager]
64
65 self.assertEqual(0, len(ids))
66
67 def test_find_no_plugins_if_there_are_no_plugins_in_plugin_managers(self):
68 plugin_manager = CompositePluginManager(
69 plugin_managers=[PluginManager(), PluginManager()]
70 )
71 ids = [plugin.id for plugin in plugin_manager]
72
73 self.assertEqual(0, len(ids))
74
75 def test_find_plugins_in_a_single_plugin_manager(self):
76 plugin_manager = CompositePluginManager(
77 plugin_managers=[
78 PluginManager(
79 plugins=[SimplePlugin(id="red"), SimplePlugin(id="yellow")]
80 )
81 ]
82 )
83 ids = [plugin.id for plugin in plugin_manager]
84
85 self.assertEqual(2, len(ids))
86 self.assertIn("red", ids)
87 self.assertIn("yellow", ids)
88
89 self._test_start_and_stop(plugin_manager, ["red", "yellow"])
90
91 def test_find_plugins_in_a_multiple_plugin_managers(self):
92 plugin_manager = CompositePluginManager(
93 plugin_managers=[
94 PluginManager(
95 plugins=[SimplePlugin(id="red"), SimplePlugin(id="yellow")]
96 ),
97 PluginManager(plugins=[SimplePlugin(id="green")]),
98 ]
99 )
100 ids = [plugin.id for plugin in plugin_manager]
101
102 self.assertEqual(3, len(ids))
103 self.assertIn("red", ids)
104 self.assertIn("yellow", ids)
105 self.assertIn("green", ids)
106
107 self._test_start_and_stop(plugin_manager, ["red", "yellow", "green"])
108
109 def test_application_gets_propogated_to_plugin_managers(self):
110 application = Application()
111
112 composite_plugin_manager = CompositePluginManager(
113 application=application,
114 plugin_managers=[PluginManager(), PluginManager()],
115 )
116
117 for plugin_manager in composite_plugin_manager.plugin_managers:
118 self.assertEqual(application, plugin_manager.application)
119
120 def test_propogate_plugin_added_or_remove_events_from_plugin_managers(
121 self,
122 ):
123 a = PluginManager()
124 b = PluginManager()
125
126 composite_plugin_manager = CompositePluginManager(
127 plugin_managers=[a, b]
128 )
129 composite_plugin_manager._plugins
130
131 def added(obj, trait_name, old, new):
132 added.count += 1
133
134 added.count = 0
135
136 composite_plugin_manager.on_trait_change(added, "plugin_added")
137
138 def removed(obj, trait_name, old, new):
139 removed.count += 1
140
141 removed.count = 0
142
143 composite_plugin_manager.on_trait_change(removed, "plugin_removed")
144
145 a.add_plugin(Plugin(id="foo"))
146 self.assertEqual(1, self._plugin_count(composite_plugin_manager))
147
148 a.remove_plugin(a.get_plugin("foo"))
149 self.assertEqual(0, self._plugin_count(composite_plugin_manager))
150
151 def test_correct_exception_propagated_from_plugin_manager(self):
152 plugin_manager = CompositePluginManager(
153 plugin_managers=[RaisingPluginManager()]
154 )
155
156 with self.assertRaises(CustomException):
157 plugin_manager.start()
158
159 #### Private protocol #####################################################
160
161 def _plugin_count(self, plugin_manager):
162 """Return how many plugins the plugin manager contains."""
163
164 count = 0
165 for plugin in plugin_manager:
166 count += 1
167
168 return count
169
170 def _test_start_and_stop(self, plugin_manager, expected):
171 """
172 Make sure the plugin manager starts and stops the expected plugins.
173 """
174
175 # Make sure the plugin manager found only the required plugins.
176 self.assertEqual(expected, [plugin.id for plugin in plugin_manager])
177
178 # Start the plugin manager. This starts all of the plugin manager's
179 # plugins.
180 plugin_manager.start()
181
182 # Make sure all of the the plugins were started.
183 for id in expected:
184 plugin = plugin_manager.get_plugin(id)
185 self.assertNotEqual(None, plugin)
186 self.assertEqual(True, plugin.started)
187
188 # Stop the plugin manager. This stops all of the plugin manager's
189 # plugins.
190 plugin_manager.stop()
191
192 # Make sure all of the the plugins were stopped.
193 for id in expected:
194 plugin = plugin_manager.get_plugin(id)
195 self.assertNotEqual(None, plugin)
196 self.assertEqual(True, plugin.stopped)