| 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 core plugin. """ |
| 11 |
|
| 12 |
# Standard library imports. |
| 13 |
import unittest |
| 14 |
|
| 15 |
# Major package imports. |
| 16 |
from pkg_resources import resource_filename |
| 17 |
|
| 18 |
from traits.api import HasTraits, Interface, List, on_trait_change, Str |
| 19 |
|
| 20 |
# Enthought library imports. |
| 21 |
from envisage.api import CorePlugin, Plugin, ServiceOffer |
| 22 |
from envisage.tests.support import SimpleApplication |
| 23 |
|
| 24 |
# This module's package. |
| 25 |
PKG = "envisage.tests" |
| 26 |
|
| 27 |
|
| 28 |
class CorePluginTestCase(unittest.TestCase): |
| 29 |
"""Tests for the core plugin.""" |
| 30 |
|
| 31 |
def test_service_offers(self): |
| 32 |
"""service offers""" |
| 33 |
|
| 34 |
class IMyService(Interface): |
| 35 |
pass |
| 36 |
|
| 37 |
class PluginA(Plugin): |
| 38 |
id = "A" |
| 39 |
|
| 40 |
service_offers = List(contributes_to="envisage.service_offers") |
| 41 |
|
| 42 |
def _service_offers_default(self): |
| 43 |
"""Trait initializer.""" |
| 44 |
|
| 45 |
service_offers = [ |
| 46 |
ServiceOffer( |
| 47 |
protocol=IMyService, factory=self._my_service_factory |
| 48 |
) |
| 49 |
] |
| 50 |
|
| 51 |
return service_offers |
| 52 |
|
| 53 |
def _my_service_factory(self, **properties): |
| 54 |
"""Service factory.""" |
| 55 |
|
| 56 |
return 42 |
| 57 |
|
| 58 |
core = CorePlugin() |
| 59 |
a = PluginA() |
| 60 |
|
| 61 |
application = SimpleApplication(plugins=[core, a]) |
| 62 |
application.start() |
| 63 |
|
| 64 |
# Lookup the service. |
| 65 |
self.assertEqual(42, application.get_service(IMyService)) |
| 66 |
|
| 67 |
# Stop the core plugin. |
| 68 |
application.stop_plugin(core) |
| 69 |
|
| 70 |
# Make sure th service has gone. |
| 71 |
self.assertEqual(None, application.get_service(IMyService)) |
| 72 |
|
| 73 |
def test_dynamically_added_service_offer(self): |
| 74 |
"""dynamically added service offer""" |
| 75 |
|
| 76 |
class IMyService(Interface): |
| 77 |
pass |
| 78 |
|
| 79 |
class PluginA(Plugin): |
| 80 |
id = "A" |
| 81 |
|
| 82 |
service_offers = List(contributes_to="envisage.service_offers") |
| 83 |
|
| 84 |
def _service_offers_default(self): |
| 85 |
"""Trait initializer.""" |
| 86 |
|
| 87 |
service_offers = [ |
| 88 |
ServiceOffer( |
| 89 |
protocol=IMyService, factory=self._my_service_factory |
| 90 |
) |
| 91 |
] |
| 92 |
|
| 93 |
return service_offers |
| 94 |
|
| 95 |
def _my_service_factory(self, **properties): |
| 96 |
"""Service factory.""" |
| 97 |
|
| 98 |
return 42 |
| 99 |
|
| 100 |
core = CorePlugin() |
| 101 |
a = PluginA() |
| 102 |
|
| 103 |
# Start off with just the core plugin. |
| 104 |
application = SimpleApplication(plugins=[core]) |
| 105 |
application.start() |
| 106 |
|
| 107 |
# Make sure the service does not exist! |
| 108 |
service = application.get_service(IMyService) |
| 109 |
self.assertIsNone(service) |
| 110 |
|
| 111 |
# Make sure the service offer exists... |
| 112 |
extensions = application.get_extensions("envisage.service_offers") |
| 113 |
self.assertEqual(0, len(extensions)) |
| 114 |
|
| 115 |
# Now add a plugin that contains a service offer. |
| 116 |
application.add_plugin(a) |
| 117 |
|
| 118 |
# Make sure the service offer exists... |
| 119 |
extensions = application.get_extensions("envisage.service_offers") |
| 120 |
self.assertEqual(1, len(extensions)) |
| 121 |
|
| 122 |
# ... and that the core plugin responded to the new service offer and |
| 123 |
# published it in the service registry. |
| 124 |
service = application.get_service(IMyService) |
| 125 |
self.assertEqual(42, service) |
| 126 |
|
| 127 |
def test_preferences(self): |
| 128 |
"""preferences""" |
| 129 |
|
| 130 |
class PluginA(Plugin): |
| 131 |
id = "A" |
| 132 |
preferences = List(contributes_to="envisage.preferences") |
| 133 |
|
| 134 |
def _preferences_default(self): |
| 135 |
"""Trait initializer.""" |
| 136 |
|
| 137 |
return ["file://" + resource_filename(PKG, "preferences.ini")] |
| 138 |
|
| 139 |
core = CorePlugin() |
| 140 |
a = PluginA() |
| 141 |
|
| 142 |
application = SimpleApplication(plugins=[core, a]) |
| 143 |
application.run() |
| 144 |
|
| 145 |
# Make sure we can get one of the preferences. |
| 146 |
self.assertEqual("42", application.preferences.get("enthought.test.x")) |
| 147 |
|
| 148 |
def test_dynamically_added_preferences(self): |
| 149 |
"""dynamically added preferences""" |
| 150 |
|
| 151 |
class PluginA(Plugin): |
| 152 |
id = "A" |
| 153 |
preferences = List(contributes_to="envisage.preferences") |
| 154 |
|
| 155 |
def _preferences_default(self): |
| 156 |
"""Trait initializer.""" |
| 157 |
|
| 158 |
return ["file://" + resource_filename(PKG, "preferences.ini")] |
| 159 |
|
| 160 |
core = CorePlugin() |
| 161 |
a = PluginA() |
| 162 |
|
| 163 |
# Start with just the core plugin. |
| 164 |
application = SimpleApplication(plugins=[core]) |
| 165 |
application.start() |
| 166 |
|
| 167 |
# Now add a plugin that contains a preference. |
| 168 |
application.add_plugin(a) |
| 169 |
|
| 170 |
# Make sure we can get one of the preferences. |
| 171 |
self.assertEqual("42", application.preferences.get("enthought.test.x")) |
| 172 |
|
| 173 |
# regression test for enthought/envisage#251 |
| 174 |
def test_unregister_service_offer(self): |
| 175 |
"""Unregister a service that is contributed to the |
| 176 |
"envisage.service_offers" extension point while the application is |
| 177 |
running. |
| 178 |
""" |
| 179 |
|
| 180 |
class IJunk(Interface): |
| 181 |
trash = Str() |
| 182 |
|
| 183 |
class Junk(HasTraits): |
| 184 |
trash = Str("garbage") |
| 185 |
|
| 186 |
class PluginA(Plugin): |
| 187 |
# The Ids of the extension points that this plugin contributes to. |
| 188 |
SERVICE_OFFERS = "envisage.service_offers" |
| 189 |
|
| 190 |
service_offers = List(contributes_to=SERVICE_OFFERS) |
| 191 |
|
| 192 |
def _service_offers_default(self): |
| 193 |
a_service_offer = ServiceOffer( |
| 194 |
protocol=IJunk, |
| 195 |
factory=self._create_junk_service, |
| 196 |
) |
| 197 |
|
| 198 |
return [a_service_offer] |
| 199 |
|
| 200 |
def _create_junk_service(self): |
| 201 |
"""Factory method for the 'Junk' service.""" |
| 202 |
|
| 203 |
return Junk() |
| 204 |
|
| 205 |
@on_trait_change("application:started") |
| 206 |
def _unregister_junk_service(self): |
| 207 |
# only 1 service is registered so it has service_id of 1 |
| 208 |
self.application.unregister_service(1) |
| 209 |
|
| 210 |
application = SimpleApplication( |
| 211 |
plugins=[CorePlugin(), PluginA()], |
| 212 |
) |
| 213 |
|
| 214 |
# Run it! |
| 215 |
application.run() |
| 216 |
|
| 217 |
def test_unregister_service(self): |
| 218 |
"""Unregister a service which was registered on the application |
| 219 |
directly, not through the CorePlugin's extension point. CorePlugin |
| 220 |
should not do anything to interfere.""" |
| 221 |
|
| 222 |
class IJunk(Interface): |
| 223 |
trash = Str() |
| 224 |
|
| 225 |
class Junk(HasTraits): |
| 226 |
trash = Str("garbage") |
| 227 |
|
| 228 |
some_junk = Junk() |
| 229 |
|
| 230 |
application = SimpleApplication( |
| 231 |
plugins=[CorePlugin()], |
| 232 |
) |
| 233 |
|
| 234 |
application.start() |
| 235 |
|
| 236 |
some_junk_id = application.register_service(IJunk, some_junk) |
| 237 |
application.unregister_service(some_junk_id) |
| 238 |
|
| 239 |
application.stop() |