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