| 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 plugins. """ |
| 11 |
|
| 12 |
|
| 13 |
import unittest |
| 14 |
|
| 15 |
# Standard library imports. |
| 16 |
from os.path import exists, join |
| 17 |
|
| 18 |
from traits.api import HasTraits, Instance, Int, Interface, List, provides |
| 19 |
|
| 20 |
# Enthought library imports. |
| 21 |
from envisage.api import Application, ExtensionPoint, IPluginActivator, Plugin |
| 22 |
from envisage.tests.ets_config_patcher import ETSConfigPatcher |
| 23 |
from envisage.tests.support import SimpleApplication |
| 24 |
|
| 25 |
|
| 26 |
class TestPlugin(Plugin): |
| 27 |
id = "test_plugin" |
| 28 |
|
| 29 |
|
| 30 |
class PluginTestCase(unittest.TestCase): |
| 31 |
"""Tests for plugins.""" |
| 32 |
|
| 33 |
def setUp(self): |
| 34 |
ets_config_patcher = ETSConfigPatcher() |
| 35 |
ets_config_patcher.start() |
| 36 |
self.addCleanup(ets_config_patcher.stop) |
| 37 |
|
| 38 |
def test_id_policy(self): |
| 39 |
"""id policy""" |
| 40 |
|
| 41 |
# If no Id is specified then use 'module_name.class_name'. |
| 42 |
p = Plugin() |
| 43 |
self.assertEqual("envisage.plugin.Plugin", p.id) |
| 44 |
|
| 45 |
# If an Id is specified make sure we use it! |
| 46 |
p = Plugin(id="wilma") |
| 47 |
self.assertEqual("wilma", p.id) |
| 48 |
|
| 49 |
# Make sure setting the name doesn't interfere with the Id. |
| 50 |
p = Plugin(name="fred", id="wilma") |
| 51 |
self.assertEqual("wilma", p.id) |
| 52 |
self.assertEqual("fred", p.name) |
| 53 |
|
| 54 |
def test_name_policy(self): |
| 55 |
"""name policy""" |
| 56 |
|
| 57 |
# If name is specified then use the plugin's class name. |
| 58 |
p = Plugin() |
| 59 |
self.assertEqual("Plugin", p.name) |
| 60 |
|
| 61 |
# If a name is specified make sure we use it! |
| 62 |
p = Plugin(name="wilma") |
| 63 |
self.assertEqual("wilma", p.name) |
| 64 |
|
| 65 |
# Try a camel case plugin class. |
| 66 |
class ThisIsMyPlugin(Plugin): |
| 67 |
pass |
| 68 |
|
| 69 |
p = ThisIsMyPlugin() |
| 70 |
self.assertEqual("This Is My Plugin", p.name) |
| 71 |
|
| 72 |
def test_plugin_activator(self): |
| 73 |
"""plugin activator.""" |
| 74 |
|
| 75 |
@provides(IPluginActivator) |
| 76 |
class NullPluginActivator(HasTraits): |
| 77 |
"""A plugin activator that does nothing!""" |
| 78 |
|
| 79 |
def start_plugin(self, plugin): |
| 80 |
"""Start a plugin.""" |
| 81 |
|
| 82 |
self.started = plugin |
| 83 |
|
| 84 |
def stop_plugin(self, plugin): |
| 85 |
"""Stop a plugin.""" |
| 86 |
|
| 87 |
self.stopped = plugin |
| 88 |
|
| 89 |
class PluginA(Plugin): |
| 90 |
id = "A" |
| 91 |
|
| 92 |
class PluginB(Plugin): |
| 93 |
id = "B" |
| 94 |
|
| 95 |
plugin_activator = NullPluginActivator() |
| 96 |
|
| 97 |
a = PluginA(activator=plugin_activator) |
| 98 |
b = PluginB() |
| 99 |
|
| 100 |
application = SimpleApplication(plugins=[a, b]) |
| 101 |
application.start() |
| 102 |
|
| 103 |
# Make sure A's plugin activator was called. |
| 104 |
self.assertEqual(a, plugin_activator.started) |
| 105 |
|
| 106 |
# Stop the application. |
| 107 |
application.stop() |
| 108 |
|
| 109 |
# Make sure A's plugin activator was called. |
| 110 |
self.assertEqual(a, plugin_activator.stopped) |
| 111 |
|
| 112 |
def test_service(self): |
| 113 |
"""service""" |
| 114 |
|
| 115 |
class Foo(HasTraits): |
| 116 |
pass |
| 117 |
|
| 118 |
class Bar(HasTraits): |
| 119 |
pass |
| 120 |
|
| 121 |
class Baz(HasTraits): |
| 122 |
pass |
| 123 |
|
| 124 |
class PluginA(Plugin): |
| 125 |
id = "A" |
| 126 |
foo = Instance(Foo, (), service=True) |
| 127 |
bar = Instance(Bar, (), service=True) |
| 128 |
baz = Instance(Baz, (), service=True) |
| 129 |
|
| 130 |
a = PluginA() |
| 131 |
|
| 132 |
application = SimpleApplication(plugins=[a]) |
| 133 |
application.start() |
| 134 |
|
| 135 |
# Make sure the services were registered. |
| 136 |
self.assertNotEqual(None, application.get_service(Foo)) |
| 137 |
self.assertEqual(a.foo, application.get_service(Foo)) |
| 138 |
|
| 139 |
self.assertNotEqual(None, application.get_service(Bar)) |
| 140 |
self.assertEqual(a.bar, application.get_service(Bar)) |
| 141 |
|
| 142 |
self.assertNotEqual(None, application.get_service(Baz)) |
| 143 |
self.assertEqual(a.baz, application.get_service(Baz)) |
| 144 |
|
| 145 |
application.stop() |
| 146 |
|
| 147 |
# Make sure the service was unregistered. |
| 148 |
self.assertEqual(None, application.get_service(Foo)) |
| 149 |
self.assertEqual(None, application.get_service(Bar)) |
| 150 |
self.assertEqual(None, application.get_service(Baz)) |
| 151 |
|
| 152 |
def test_service_protocol(self): |
| 153 |
"""service protocol""" |
| 154 |
|
| 155 |
class IFoo(Interface): |
| 156 |
pass |
| 157 |
|
| 158 |
class IBar(Interface): |
| 159 |
pass |
| 160 |
|
| 161 |
@provides(IFoo, IBar) |
| 162 |
class Foo(HasTraits): |
| 163 |
pass |
| 164 |
|
| 165 |
class PluginA(Plugin): |
| 166 |
id = "A" |
| 167 |
foo = Instance(Foo, (), service=True, service_protocol=IBar) |
| 168 |
|
| 169 |
a = PluginA() |
| 170 |
|
| 171 |
application = SimpleApplication(plugins=[a]) |
| 172 |
application.start() |
| 173 |
|
| 174 |
# Make sure the service was registered with the 'IBar' protocol. |
| 175 |
self.assertNotEqual(None, application.get_service(IBar)) |
| 176 |
self.assertEqual(a.foo, application.get_service(IBar)) |
| 177 |
|
| 178 |
application.stop() |
| 179 |
|
| 180 |
# Make sure the service was unregistered. |
| 181 |
self.assertEqual(None, application.get_service(IBar)) |
| 182 |
|
| 183 |
def test_multiple_trait_contributions(self): |
| 184 |
"""multiple trait contributions""" |
| 185 |
|
| 186 |
class PluginA(Plugin): |
| 187 |
id = "A" |
| 188 |
x = ExtensionPoint(List, id="x") |
| 189 |
|
| 190 |
class PluginB(Plugin): |
| 191 |
id = "B" |
| 192 |
|
| 193 |
x = List([1, 2, 3], contributes_to="x") |
| 194 |
y = List([4, 5, 6], contributes_to="x") |
| 195 |
|
| 196 |
a = PluginA() |
| 197 |
b = PluginB() |
| 198 |
|
| 199 |
application = SimpleApplication(plugins=[a, b]) |
| 200 |
|
| 201 |
# We should get an error because the plugin has multiple traits |
| 202 |
# contributing to the same extension point. |
| 203 |
with self.assertRaises(ValueError): |
| 204 |
application.get_extensions("x") |
| 205 |
|
| 206 |
def test_exception_in_trait_contribution(self): |
| 207 |
"""exception in trait contribution""" |
| 208 |
|
| 209 |
class PluginA(Plugin): |
| 210 |
id = "A" |
| 211 |
x = ExtensionPoint(List, id="x") |
| 212 |
|
| 213 |
class PluginB(Plugin): |
| 214 |
id = "B" |
| 215 |
|
| 216 |
x = List(contributes_to="x") |
| 217 |
|
| 218 |
def _x_default(self): |
| 219 |
"""Trait initializer.""" |
| 220 |
|
| 221 |
raise 1 / 0 |
| 222 |
|
| 223 |
a = PluginA() |
| 224 |
b = PluginB() |
| 225 |
|
| 226 |
application = SimpleApplication(plugins=[a, b]) |
| 227 |
|
| 228 |
# We should get an when we try to get the contributions to the |
| 229 |
# extension point. |
| 230 |
with self.assertRaises(ZeroDivisionError): |
| 231 |
application.get_extensions("x") |
| 232 |
|
| 233 |
def test_contributes_to(self): |
| 234 |
"""contributes to""" |
| 235 |
|
| 236 |
class PluginA(Plugin): |
| 237 |
id = "A" |
| 238 |
x = ExtensionPoint(List, id="x") |
| 239 |
|
| 240 |
class PluginB(Plugin): |
| 241 |
id = "B" |
| 242 |
x = List([1, 2, 3], contributes_to="x") |
| 243 |
|
| 244 |
a = PluginA() |
| 245 |
b = PluginB() |
| 246 |
|
| 247 |
application = SimpleApplication(plugins=[a, b]) |
| 248 |
|
| 249 |
# We should get an error because the plugin has multiple traits |
| 250 |
# contributing to the same extension point. |
| 251 |
self.assertEqual([1, 2, 3], application.get_extensions("x")) |
| 252 |
|
| 253 |
def test_add_plugins_to_empty_application(self): |
| 254 |
"""add plugins to empty application""" |
| 255 |
|
| 256 |
class PluginA(Plugin): |
| 257 |
id = "A" |
| 258 |
x = ExtensionPoint(List(Int), id="x") |
| 259 |
|
| 260 |
def _x_items_changed(self, event): |
| 261 |
self.added = event.added |
| 262 |
self.removed = event.removed |
| 263 |
|
| 264 |
class PluginB(Plugin): |
| 265 |
id = "B" |
| 266 |
x = List(Int, [1, 2, 3], contributes_to="x") |
| 267 |
|
| 268 |
class PluginC(Plugin): |
| 269 |
id = "C" |
| 270 |
x = List(Int, [4, 5, 6], contributes_to="x") |
| 271 |
|
| 272 |
a = PluginA() |
| 273 |
b = PluginB() |
| 274 |
c = PluginC() |
| 275 |
|
| 276 |
# Create an empty application. |
| 277 |
application = SimpleApplication() |
| 278 |
application.start() |
| 279 |
|
| 280 |
# Add the plugin that offers the extension point. |
| 281 |
application.add_plugin(a) |
| 282 |
|
| 283 |
####################################################################### |
| 284 |
# fixme: Currently, we connect up extension point traits when the |
| 285 |
# plugin is started. Is this right? Should we start plugins by default |
| 286 |
# when we add them (and maybe have the ability to add a plugin without |
| 287 |
# starting it?). |
| 288 |
# |
| 289 |
# I think we should start the plugin, otherwise you have the wierdness |
| 290 |
# that the extension contributed by the plugin are available after |
| 291 |
# the call to 'add_plugin', but the plugin isn't started?!? |
| 292 |
####################################################################### |
| 293 |
|
| 294 |
application.start_plugin(a) |
| 295 |
|
| 296 |
####################################################################### |
| 297 |
# fixme: Currently, we only fire changed events if an extension point |
| 298 |
# has already been accessed! Is this right? |
| 299 |
####################################################################### |
| 300 |
|
| 301 |
self.assertEqual([], a.x) |
| 302 |
|
| 303 |
# Add a plugin that contributes to the extension point. |
| 304 |
application.add_plugin(b) |
| 305 |
|
| 306 |
# Make sure that we pick up B's extensions and that the appropriate |
| 307 |
# trait event was fired. |
| 308 |
self.assertEqual([1, 2, 3], a.x) |
| 309 |
self.assertEqual([1, 2, 3], a.added) |
| 310 |
|
| 311 |
# Add another plugin that contributes to the extension point. |
| 312 |
application.add_plugin(c) |
| 313 |
|
| 314 |
self.assertEqual([1, 2, 3, 4, 5, 6], a.x) |
| 315 |
self.assertEqual([4, 5, 6], a.added) |
| 316 |
|
| 317 |
# Remove the first contributing plugin. |
| 318 |
application.remove_plugin(b) |
| 319 |
|
| 320 |
self.assertEqual([4, 5, 6], a.x) |
| 321 |
self.assertEqual([1, 2, 3], a.removed) |
| 322 |
|
| 323 |
# Remove the second contributing plugin. |
| 324 |
application.remove_plugin(c) |
| 325 |
|
| 326 |
self.assertEqual([], a.x) |
| 327 |
self.assertEqual([4, 5, 6], a.removed) |
| 328 |
|
| 329 |
def test_home(self): |
| 330 |
"""home""" |
| 331 |
|
| 332 |
class PluginA(Plugin): |
| 333 |
id = "A" |
| 334 |
|
| 335 |
class PluginB(Plugin): |
| 336 |
id = "B" |
| 337 |
|
| 338 |
a = PluginA() |
| 339 |
b = PluginB() |
| 340 |
|
| 341 |
application = SimpleApplication(plugins=[a, b]) |
| 342 |
|
| 343 |
# Make sure that each plugin gets its own directory. |
| 344 |
self.assertEqual(join(application.home, "plugins", a.id), a.home) |
| 345 |
self.assertEqual(join(application.home, "plugins", b.id), b.home) |
| 346 |
|
| 347 |
# Make sure that the directories got created. |
| 348 |
self.assertTrue(exists(a.home)) |
| 349 |
self.assertTrue(exists(b.home)) |
| 350 |
|
| 351 |
# Create a new application with plugins with the same Id to make sure |
| 352 |
# that it all works when the directories already exist. |
| 353 |
a = PluginA() |
| 354 |
b = PluginB() |
| 355 |
|
| 356 |
application = SimpleApplication(plugins=[a, b]) |
| 357 |
|
| 358 |
# Make sure that each plugin gets its own directory. |
| 359 |
self.assertEqual(join(application.home, "plugins", a.id), a.home) |
| 360 |
self.assertEqual(join(application.home, "plugins", b.id), b.home) |
| 361 |
|
| 362 |
# Make sure the directories got created. |
| 363 |
self.assertTrue(exists(a.home)) |
| 364 |
self.assertTrue(exists(b.home)) |
| 365 |
|
| 366 |
def test_no_recursion(self): |
| 367 |
"""Regression test for #119.""" |
| 368 |
|
| 369 |
class PluginA(Plugin): |
| 370 |
id = "A" |
| 371 |
x = ExtensionPoint(List, id="bob") |
| 372 |
|
| 373 |
application = Application(plugins=[PluginA()]) |
| 374 |
application.get_extensions("bob") |
| 375 |
|
| 376 |
def test_plugin_str_representation(self): |
| 377 |
"""test the string representation of the plugin""" |
| 378 |
plugin_repr = "TestPlugin(id={!r}, name={!r})" |
| 379 |
plugin = TestPlugin(id="Fred", name="Wilma") |
| 380 |
self.assertEqual(str(plugin), plugin_repr.format("Fred", "Wilma")) |
| 381 |
self.assertEqual(repr(plugin), plugin_repr.format("Fred", "Wilma")) |