| 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 extension point bindings. """ |
| 11 |
|
| 12 |
# Standard library imports. |
| 13 |
import unittest |
| 14 |
import weakref |
| 15 |
|
| 16 |
from traits.api import Any, HasTraits, List |
| 17 |
|
| 18 |
# Enthought library imports. |
| 19 |
from envisage.api import ( |
| 20 |
bind_extension_point, |
| 21 |
ExtensionPoint, |
| 22 |
unbind_extension_point, |
| 23 |
) |
| 24 |
|
| 25 |
# Local imports. |
| 26 |
from envisage.tests.mutable_extension_registry import MutableExtensionRegistry |
| 27 |
|
| 28 |
|
| 29 |
class BindingTarget(HasTraits): |
| 30 |
""" |
| 31 |
Example class whose traits are used as a binding target. |
| 32 |
""" |
| 33 |
|
| 34 |
#: Target trait for extension point binding. |
| 35 |
target = List(Any()) |
| 36 |
|
| 37 |
|
| 38 |
class ExtensionPointBindingTestCase(unittest.TestCase): |
| 39 |
"""Tests for extension point binding.""" |
| 40 |
|
| 41 |
def setUp(self): |
| 42 |
self.extension_registry = MutableExtensionRegistry() |
| 43 |
|
| 44 |
def tearDown(self): |
| 45 |
del self.extension_registry |
| 46 |
|
| 47 |
def test_untyped_extension_point(self): |
| 48 |
"""untyped extension point""" |
| 49 |
|
| 50 |
registry = self.extension_registry |
| 51 |
|
| 52 |
# Add an extension point. |
| 53 |
registry.add_extension_point(self._create_extension_point("my.ep")) |
| 54 |
|
| 55 |
# Add an extension. |
| 56 |
registry.add_extension("my.ep", 42) |
| 57 |
|
| 58 |
# Declare a class that consumes the extension. |
| 59 |
class Foo(HasTraits): |
| 60 |
x = List |
| 61 |
|
| 62 |
events = [] |
| 63 |
f = Foo() |
| 64 |
f.observe(events.append, "x_items") |
| 65 |
|
| 66 |
# Make some bindings. |
| 67 |
bind_extension_point(f, "x", "my.ep", registry) |
| 68 |
|
| 69 |
# Make sure that the object was initialized properly. |
| 70 |
self.assertEqual(1, len(f.x)) |
| 71 |
self.assertEqual(42, f.x[0]) |
| 72 |
|
| 73 |
# Add another extension. |
| 74 |
registry.add_extension("my.ep", "a string") |
| 75 |
|
| 76 |
# Make sure that the object picked up the new extension... |
| 77 |
self.assertEqual(2, len(f.x)) |
| 78 |
self.assertTrue(42 in f.x) |
| 79 |
self.assertTrue("a string" in f.x) |
| 80 |
|
| 81 |
# ... and that the correct trait change event was fired. |
| 82 |
self.assertEqual(len(events), 1) |
| 83 |
event = events[0] |
| 84 |
self.assertEqual(f, event.object) |
| 85 |
self.assertEqual("x_items", event.name) |
| 86 |
self.assertEqual(1, len(event.new.added)) |
| 87 |
self.assertTrue("a string" in event.new.added) |
| 88 |
|
| 89 |
def test_set_extensions_via_trait(self): |
| 90 |
"""set extensions via trait""" |
| 91 |
|
| 92 |
registry = self.extension_registry |
| 93 |
|
| 94 |
# Add an extension point. |
| 95 |
registry.add_extension_point(self._create_extension_point("my.ep")) |
| 96 |
|
| 97 |
# Add an extension. |
| 98 |
registry.add_extension("my.ep", 42) |
| 99 |
|
| 100 |
# Declare a class that consumes the extension. |
| 101 |
class Foo(HasTraits): |
| 102 |
x = List |
| 103 |
|
| 104 |
events = [] |
| 105 |
f = Foo() |
| 106 |
f.observe(events.append, "*") |
| 107 |
|
| 108 |
# Make some bindings. |
| 109 |
bind_extension_point(f, "x", "my.ep", registry) |
| 110 |
|
| 111 |
# Make sure that the object was initialized properly. |
| 112 |
self.assertEqual(1, len(f.x)) |
| 113 |
self.assertEqual(42, f.x[0]) |
| 114 |
|
| 115 |
# Set the extensions. |
| 116 |
f.x = ["a string"] |
| 117 |
|
| 118 |
# Make sure that the object picked up the new extension... |
| 119 |
self.assertEqual(1, len(f.x)) |
| 120 |
self.assertTrue("a string" in f.x) |
| 121 |
|
| 122 |
self.assertEqual(1, len(registry.get_extensions("my.ep"))) |
| 123 |
self.assertTrue("a string" in registry.get_extensions("my.ep")) |
| 124 |
|
| 125 |
# ... and that the correct trait change event was fired. |
| 126 |
self.assertEqual(len(events), 1) |
| 127 |
event = events[0] |
| 128 |
self.assertEqual(f, event.object) |
| 129 |
self.assertEqual("x", event.name) |
| 130 |
self.assertEqual(1, len(event.new)) |
| 131 |
self.assertTrue("a string" in event.new) |
| 132 |
|
| 133 |
def test_set_extensions_via_registry(self): |
| 134 |
"""set extensions via registry""" |
| 135 |
|
| 136 |
registry = self.extension_registry |
| 137 |
|
| 138 |
# Add an extension point. |
| 139 |
registry.add_extension_point(self._create_extension_point("my.ep")) |
| 140 |
|
| 141 |
# Add an extension. |
| 142 |
registry.add_extension("my.ep", 42) |
| 143 |
|
| 144 |
# Declare a class that consumes the extension. |
| 145 |
class Foo(HasTraits): |
| 146 |
x = List |
| 147 |
|
| 148 |
events = [] |
| 149 |
f = Foo() |
| 150 |
f.observe(events.append, "*") |
| 151 |
|
| 152 |
# Make some bindings. |
| 153 |
bind_extension_point(f, "x", "my.ep", registry) |
| 154 |
|
| 155 |
# Make sure that the object was initialized properly. |
| 156 |
self.assertEqual(1, len(f.x)) |
| 157 |
self.assertEqual(42, f.x[0]) |
| 158 |
|
| 159 |
# Set the extensions. |
| 160 |
registry.set_extensions("my.ep", ["a string"]) |
| 161 |
|
| 162 |
# Make sure that the object picked up the new extension... |
| 163 |
self.assertEqual(1, len(f.x)) |
| 164 |
self.assertTrue("a string" in f.x) |
| 165 |
|
| 166 |
# ... and that the correct trait change event was fired. |
| 167 |
self.assertEqual(len(events), 1) |
| 168 |
event = events[0] |
| 169 |
self.assertEqual(f, event.object) |
| 170 |
self.assertEqual("x", event.name) |
| 171 |
self.assertEqual(1, len(event.new)) |
| 172 |
self.assertTrue("a string" in event.new) |
| 173 |
|
| 174 |
def test_explicit_extension_registry(self): |
| 175 |
"""explicit extension registry""" |
| 176 |
|
| 177 |
registry = self.extension_registry |
| 178 |
|
| 179 |
# Add an extension point. |
| 180 |
registry.add_extension_point(self._create_extension_point("my.ep")) |
| 181 |
|
| 182 |
# Add an extension. |
| 183 |
registry.add_extension("my.ep", 42) |
| 184 |
|
| 185 |
# Declare a class that consumes the extension. |
| 186 |
class Foo(HasTraits): |
| 187 |
x = List |
| 188 |
|
| 189 |
f = Foo() |
| 190 |
|
| 191 |
# Create an empty extension registry use that in the binding. |
| 192 |
extension_registry = MutableExtensionRegistry() |
| 193 |
|
| 194 |
# Make some bindings. |
| 195 |
bind_extension_point(f, "x", "my.ep", extension_registry) |
| 196 |
|
| 197 |
# Make sure that we pick up the empty extension registry and not the |
| 198 |
# default one. |
| 199 |
self.assertEqual(0, len(f.x)) |
| 200 |
|
| 201 |
def test_should_be_able_to_bind_multiple_traits_on_a_single_object(self): |
| 202 |
registry = self.extension_registry |
| 203 |
|
| 204 |
# Add 2 extension points. |
| 205 |
registry.add_extension_point(self._create_extension_point("my.ep")) |
| 206 |
registry.add_extension_point( |
| 207 |
self._create_extension_point("another.ep") |
| 208 |
) |
| 209 |
|
| 210 |
# Declare a class that consumes both of the extension points. |
| 211 |
class Foo(HasTraits): |
| 212 |
x = List |
| 213 |
y = List |
| 214 |
|
| 215 |
f = Foo() |
| 216 |
|
| 217 |
# Bind two different traits on the object to the extension points. |
| 218 |
bind_extension_point(f, "x", "my.ep", registry) |
| 219 |
bind_extension_point(f, "y", "another.ep", registry) |
| 220 |
self.assertEqual(0, len(f.x)) |
| 221 |
self.assertEqual(0, len(f.y)) |
| 222 |
|
| 223 |
# Add some contributions to the extension points. |
| 224 |
registry.add_extension("my.ep", 42) |
| 225 |
registry.add_extensions("another.ep", [98, 99, 100]) |
| 226 |
|
| 227 |
# Make sure both traits were bound correctly. |
| 228 |
self.assertEqual(1, len(f.x)) |
| 229 |
self.assertEqual(3, len(f.y)) |
| 230 |
|
| 231 |
def test_unbind_extension_point(self): |
| 232 |
# Given ... |
| 233 |
# ... an extension point ... |
| 234 |
registry = self.extension_registry |
| 235 |
registry.add_extension_point(self._create_extension_point("my.ep")) |
| 236 |
|
| 237 |
# ... and an object with a corresponding trait ... |
| 238 |
target = BindingTarget() |
| 239 |
|
| 240 |
# When we bind the extension point to the trait ... |
| 241 |
bind_extension_point(target, "target", "my.ep", registry) |
| 242 |
|
| 243 |
# Then contributions to the extension point modify the trait. |
| 244 |
registry.add_extension("my.ep", "a string") |
| 245 |
self.assertEqual(target.target, ["a string"]) |
| 246 |
|
| 247 |
# When we unbind the extension point |
| 248 |
unbind_extension_point(target, "target", "my.ep", registry) |
| 249 |
|
| 250 |
# Then contributions no longer change the trait. |
| 251 |
registry.add_extension("my.ep", "another string") |
| 252 |
self.assertEqual(target.target, ["a string"]) |
| 253 |
|
| 254 |
def test_unbinding_removes_references(self): |
| 255 |
# Given an extension point bound to a trait |
| 256 |
extension_point = self._create_extension_point("my.ep") |
| 257 |
self.extension_registry.add_extension_point(extension_point) |
| 258 |
target = BindingTarget() |
| 259 |
bind_extension_point( |
| 260 |
target, "target", "my.ep", self.extension_registry |
| 261 |
) |
| 262 |
|
| 263 |
# Use a weakref finalizer to keep track of whether 'target' still |
| 264 |
# has references keeping it alive. |
| 265 |
target_monitor = weakref.finalize(target, lambda: None) |
| 266 |
|
| 267 |
# When we unbind and delete the target object |
| 268 |
unbind_extension_point( |
| 269 |
target, "target", "my.ep", self.extension_registry |
| 270 |
) |
| 271 |
del target |
| 272 |
|
| 273 |
# Then 'target' should no longer be alive. |
| 274 |
self.assertFalse(target_monitor.alive) |
| 275 |
|
| 276 |
########################################################################### |
| 277 |
# Private interface. |
| 278 |
########################################################################### |
| 279 |
|
| 280 |
def _create_extension_point(self, id, trait_type=List, desc=""): |
| 281 |
"""Create an extension point.""" |
| 282 |
|
| 283 |
return ExtensionPoint(id=id, trait_type=trait_type, desc=desc) |