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