File: plugin_test_case.py

package info (click to toggle)
python-envisagecore 3.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,096 kB
  • ctags: 1,063
  • sloc: python: 4,115; makefile: 7; sh: 5
file content (457 lines) | stat: -rw-r--r-- 12,995 bytes parent folder | download | duplicates (2)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
""" Tests for plugins. """


# Standard library imports.
import random, unittest
from os.path import exists, join

# Enthought library imports.
from enthought.envisage.api import Application, ExtensionPoint
from enthought.envisage.api import IPluginActivator, Plugin, contributes_to
from enthought.traits.api import HasTraits, Instance, Int, Interface, List, Str
from enthought.traits.api import implements


def listener(obj, trait_name, old, new):
    """ A useful trait change handler for testing! """

    listener.obj = obj
    listener.trait_name = trait_name
    listener.old = old
    listener.new = new

    return


class TestApplication(Application):
    """ The type of application used in the tests. """

    id = 'test'

    
class PluginTestCase(unittest.TestCase):
    """ Tests for plugins. """

    ###########################################################################
    # '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_id_policy(self):
        """ id policy """

        # If no Id is specified then use 'module_name.class_name'.
        p = Plugin()
        self.assertEqual('enthought.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)

        return

    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)
        
        return
    
    def test_plugin_activator(self):
        """ plugin activator. """

        class NullPluginActivator(HasTraits):
            """ A plugin activator that does nothing! """

            implements(IPluginActivator)
            
            def start_plugin(self, plugin):
                """ Start a plugin. """

                self.started = plugin

                return
            
            def stop_plugin(self, plugin):
                """ Stop a plugin. """
                
                self.stopped = plugin

                return

        class PluginA(Plugin):
            id = 'A'

        class PluginB(Plugin):
            id = 'B'

        plugin_activator = NullPluginActivator()

        a = PluginA(activator=plugin_activator)
        b = PluginB()

        application = TestApplication(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)

        return
    
    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 = TestApplication(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))

        return

    def test_service_protocol(self):
        """ service protocol """

        class IFoo(Interface):
            pass

        class IBar(Interface):
            pass
        
        class Foo(HasTraits):
            implements(IFoo, IBar)

        class PluginA(Plugin):
            id = 'A'
            foo = Instance(Foo, (), service=True, service_protocol=IBar)
            
        a = PluginA()
        
        application = TestApplication(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))

        return

    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 = TestApplication(plugins=[a, b])

        # We should get an error because the plugin has multiple traits
        # contributing to the same extension point.
        self.failUnlessRaises(ValueError, application.get_extensions, 'x')

        return

    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 = TestApplication(plugins=[a, b])

        # We should get an when we try to get the contributions to the
        # extension point.
        self.failUnlessRaises(
            ZeroDivisionError, application.get_extensions, 'x'
        )

        return

    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 = TestApplication(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'))

        return

    def test_contributes_to_decorator(self):
        """ contributes to decorator """

        class PluginA(Plugin):
            id = 'A'
            x  = ExtensionPoint(List, id='x')

        class PluginB(Plugin):
            id = 'B'

            @contributes_to('x')
            def _x_contributions(self):
                return [1, 2, 3]
            
        a = PluginA()
        b = PluginB()

        application = TestApplication(plugins=[a, b])
        self.assertEqual([1, 2, 3], application.get_extensions('x'))

        return

    def test_contributes_to_decorator_ignored_if_trait_present(self):
        """ contributes to decorator ignored if trait present """

        class PluginA(Plugin):
            id = 'A'
            x  = ExtensionPoint(List, id='x')

        class PluginB(Plugin):
            id = 'B'
            x  = List([1, 2, 3], contributes_to='x')

            @contributes_to('x')
            def _x_contributions(self):
                return [4, 5, 6]
            
        a = PluginA()
        b = PluginB()

        application = TestApplication(plugins=[a, b])
        self.assertEqual([1, 2, 3], application.get_extensions('x'))

        return

    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

                return

        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 = TestApplication()
        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)

        return

    def test_home(self):
        """ home """

        class PluginA(Plugin):
            id = 'A'

        class PluginB(Plugin):
            id = 'B'

        a = PluginA()
        b = PluginB()

        application = TestApplication(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.assert_(exists(a.home))
        self.assert_(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 = TestApplication(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.assert_(exists(a.home))
        self.assert_(exists(b.home))
        
        return


# Entry point for stand-alone testing.
if __name__ == '__main__':
    unittest.main()

#### EOF ######################################################################