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
|
""" Tests for the core plugin. """
# Standard library imports.
import os.path
import unittest
# Major package imports.
from pkg_resources import resource_filename
# Enthought library imports.
from enthought.envisage.api import Application, Category, ClassLoadHook, Plugin
from enthought.envisage.api import ServiceOffer
from enthought.traits.api import HasTraits, Int, Interface, List
# This module's package.
PKG = 'enthought.envisage.tests'
class TestApplication(Application):
""" The type of application used in the tests. """
id = 'core.plugin.test'
class CorePluginTestCase(unittest.TestCase):
""" Tests for the core plugin. """
###########################################################################
# 'TestCase' interface.
###########################################################################
def setUp(self):
""" Prepares the test fixture before each test method is called. """
return
def tearDown(self):
""" Called immediately after each test method has been called. """
return
###########################################################################
# Tests.
###########################################################################
def test_service_offers(self):
""" service offers """
from enthought.envisage.core_plugin import CorePlugin
class IMyService(Interface):
pass
class PluginA(Plugin):
id = 'A'
service_offers = List(
contributes_to='enthought.envisage.service_offers'
)
def _service_offers_default(self):
""" Trait initializer. """
service_offers = [
ServiceOffer(
protocol=IMyService, factory=self._my_service_factory
)
]
return service_offers
def _my_service_factory(self, **properties):
""" Service factory. """
return 42
core = CorePlugin()
a = PluginA()
application = TestApplication(plugins=[core, a])
application.start()
# Lookup the service.
self.assertEqual(42, application.get_service(IMyService))
# Stop the core plugin.
application.stop_plugin(core)
# Make sure th service has gone.
self.assertEqual(None, application.get_service(IMyService))
return
def test_categories(self):
""" categories """
from enthought.envisage.core_plugin import CorePlugin
class PluginA(Plugin):
id = 'A'
categories = List(contributes_to='enthought.envisage.categories')
def _categories_default(self):
""" Trait initializer. """
bar_category = Category(
class_name = PKG + '.bar_category.BarCategory',
target_class_name = CorePluginTestCase.__module__ + '.Bar'
)
return [bar_category]
core = CorePlugin()
a = PluginA()
application = TestApplication(plugins=[core, a])
application.start()
# Create the target class.
class Bar(HasTraits):
x = Int
# Make sure the category was imported and added.
#
# fixme: The following assertion was commented out. Please don't do
# that! If a test fails we need to work out why - otherwise you have
# just completely removed the benefits of having tests in the first
# place! This test works for me on Python 2.4!
self.assert_('y' in Bar.class_traits())
return
def test_class_load_hooks(self):
""" class load hooks """
from enthought.envisage.core_plugin import CorePlugin
def on_class_loaded(cls):
""" Called when a class has been loaded. """
on_class_loaded.cls = cls
return
class PluginA(Plugin):
id = 'A'
class_load_hooks = List(
[
ClassLoadHook(
class_name = CorePluginTestCase.__module__ + '.Baz',
on_load = on_class_loaded,
)
],
contributes_to='enthought.envisage.class_load_hooks'
)
core = CorePlugin()
a = PluginA()
application = TestApplication(plugins=[core, a])
application.start()
# Make sure we ignore a class that we are not interested in!
class Bif(HasTraits):
pass
# Make sure the class load hook was *ignored*.
self.assert_(not hasattr(on_class_loaded, 'cls'))
# Create the target class.
class Baz(HasTraits):
pass
# Make sure the class load hook was called.
#
# fixme: The following assertion was commented out. Please don't do
# that! If a test fails we need to work out why - otherwise you have
# just completely removed the benefits of having tests in the first
# place! This test works for me on Python 2.4!
self.assertEqual(Baz, on_class_loaded.cls)
return
def test_preferences(self):
""" preferences """
# The core plugin is the plugin that offers the preferences extension
# point.
from enthought.envisage.core_plugin import CorePlugin
class PluginA(Plugin):
id = 'A'
preferences = List(contributes_to='enthought.envisage.preferences')
def _preferences_default(self):
""" Trait initializer. """
return ['file://' + resource_filename(PKG, 'preferences.ini')]
application = TestApplication(plugins=[CorePlugin(), PluginA()])
application.run()
# Make sure we can get one of the preferences.
self.assertEqual('42', application.preferences.get('enthought.test.x'))
return
# Entry point for stand-alone testing.
if __name__ == '__main__':
unittest.main()
#### EOF ######################################################################
|