~ubuntu-branches/ubuntu/utopic/python-apptools/utopic

« back to all changes in this revision

Viewing changes to apptools/preferences/tests/preferences_helper_test_case.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-08 23:55:50 UTC
  • mfrom: (2.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110708235550-yz5u79ubeo4dhyfx
Tags: 4.0.0-1
* New upstream release
* Update debian/watch file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
""" Tests for the preferences helper. """
 
2
 
 
3
 
 
4
# Standard library imports.
 
5
import unittest
 
6
 
 
7
# Major package imports.
 
8
from pkg_resources import resource_filename
 
9
 
 
10
# Enthought library imports.
 
11
from apptools.preferences.api import Preferences, PreferencesHelper
 
12
from apptools.preferences.api import ScopedPreferences
 
13
from apptools.preferences.api import set_default_preferences
 
14
from traits.api import Any, Bool, HasTraits, Int, Float, List, Str
 
15
from traits.api import Unicode
 
16
 
 
17
 
 
18
def listener(obj, trait_name, old, new):
 
19
    """ A useful trait change handler for testing! """
 
20
 
 
21
    listener.obj = obj
 
22
    listener.trait_name = trait_name
 
23
    listener.old = old
 
24
    listener.new = new
 
25
 
 
26
    return
 
27
 
 
28
 
 
29
# This module's package.
 
30
PKG = 'apptools.preferences.tests'
 
31
 
 
32
 
 
33
class PreferencesHelperTestCase(unittest.TestCase):
 
34
    """ Tests for the preferences helper. """
 
35
 
 
36
    ###########################################################################
 
37
    # 'TestCase' interface.
 
38
    ###########################################################################
 
39
 
 
40
    def setUp(self):
 
41
        """ Prepares the test fixture before each test method is called. """
 
42
 
 
43
        self.preferences = set_default_preferences(Preferences())
 
44
 
 
45
        # The filename of the example preferences file.
 
46
        self.example = resource_filename(PKG, 'example.ini')
 
47
 
 
48
        return
 
49
 
 
50
    def tearDown(self):
 
51
        """ Called immediately after each test method has been called. """
 
52
 
 
53
        return
 
54
 
 
55
    ###########################################################################
 
56
    # Tests.
 
57
    ###########################################################################
 
58
 
 
59
    def test_class_scope_preferences_path(self):
 
60
        """ class scope preferences path """
 
61
 
 
62
        p = self.preferences
 
63
        p.load(self.example)
 
64
 
 
65
        class AcmeUIPreferencesHelper(PreferencesHelper):
 
66
            """ A helper! """
 
67
 
 
68
            # The path to the preferences node that contains our preferences.
 
69
            preferences_path = 'acme.ui'
 
70
 
 
71
            # The traits that we want to initialize from preferences.
 
72
            bgcolor     = Str
 
73
            width       = Int
 
74
            ratio       = Float
 
75
            visible     = Bool
 
76
            description = Unicode
 
77
            offsets     = List(Int)
 
78
            names       = List(Str)
 
79
 
 
80
        helper = AcmeUIPreferencesHelper()
 
81
        helper.on_trait_change(listener)
 
82
 
 
83
        # Make sure the helper was initialized properly.
 
84
        self.assertEqual('blue', helper.bgcolor)
 
85
        self.assertEqual(50, helper.width)
 
86
        self.assertEqual(1.0, helper.ratio)
 
87
        self.assertEqual(True, helper.visible)
 
88
        self.assertEqual(u'acme ui', helper.description)
 
89
        self.assertEqual([1, 2, 3, 4], helper.offsets)
 
90
        self.assertEqual(['joe', 'fred', 'jane'], helper.names)
 
91
 
 
92
        # Make sure we can set the preference via the helper...
 
93
        helper.bgcolor = 'yellow'
 
94
        self.assertEqual('yellow', p.get('acme.ui.bgcolor'))
 
95
        self.assertEqual('yellow', helper.bgcolor)
 
96
 
 
97
        # ... and that the correct trait change event was fired.
 
98
        self.assertEqual(helper, listener.obj)
 
99
        self.assertEqual('bgcolor', listener.trait_name)
 
100
        self.assertEqual('blue', listener.old)
 
101
        self.assertEqual('yellow', listener.new)
 
102
 
 
103
        # Make sure we can set the preference via the preferences node...
 
104
        p.set('acme.ui.bgcolor', 'red')
 
105
        self.assertEqual('red', p.get('acme.ui.bgcolor'))
 
106
        self.assertEqual('red', helper.bgcolor)
 
107
 
 
108
        # ... and that the correct trait change event was fired.
 
109
        self.assertEqual(helper, listener.obj)
 
110
        self.assertEqual('bgcolor', listener.trait_name)
 
111
        self.assertEqual('yellow', listener.old)
 
112
        self.assertEqual('red', listener.new)
 
113
 
 
114
        return
 
115
 
 
116
    def test_instance_scope_preferences_path(self):
 
117
        """ instance scope preferences path """
 
118
 
 
119
        p = self.preferences
 
120
        p.load(self.example)
 
121
 
 
122
        class AcmeUIPreferencesHelper(PreferencesHelper):
 
123
            """ A helper! """
 
124
 
 
125
            # The traits that we want to initialize from preferences.
 
126
            bgcolor     = Str
 
127
            width       = Int
 
128
            ratio       = Float
 
129
            visible     = Bool
 
130
            description = Unicode
 
131
            offsets     = List(Int)
 
132
            names       = List(Str)
 
133
 
 
134
        helper = AcmeUIPreferencesHelper(preferences_path='acme.ui')
 
135
        helper.on_trait_change(listener)
 
136
 
 
137
        # Make sure the helper was initialized properly.
 
138
        self.assertEqual('blue', helper.bgcolor)
 
139
        self.assertEqual(50, helper.width)
 
140
        self.assertEqual(1.0, helper.ratio)
 
141
        self.assertEqual(True, helper.visible)
 
142
        self.assertEqual(u'acme ui', helper.description)
 
143
        self.assertEqual([1, 2, 3, 4], helper.offsets)
 
144
        self.assertEqual(['joe', 'fred', 'jane'], helper.names)
 
145
 
 
146
        # Make sure we can set the preference via the helper...
 
147
        helper.bgcolor = 'yellow'
 
148
        self.assertEqual('yellow', p.get('acme.ui.bgcolor'))
 
149
        self.assertEqual('yellow', helper.bgcolor)
 
150
 
 
151
        # ... and that the correct trait change event was fired.
 
152
        self.assertEqual(helper, listener.obj)
 
153
        self.assertEqual('bgcolor', listener.trait_name)
 
154
        self.assertEqual('blue', listener.old)
 
155
        self.assertEqual('yellow', listener.new)
 
156
 
 
157
        # Make sure we can set the preference via the preferences node...
 
158
        p.set('acme.ui.bgcolor', 'red')
 
159
        self.assertEqual('red', p.get('acme.ui.bgcolor'))
 
160
        self.assertEqual('red', helper.bgcolor)
 
161
 
 
162
        # ... and that the correct trait change event was fired.
 
163
        self.assertEqual(helper, listener.obj)
 
164
        self.assertEqual('bgcolor', listener.trait_name)
 
165
        self.assertEqual('yellow', listener.old)
 
166
        self.assertEqual('red', listener.new)
 
167
 
 
168
        return
 
169
 
 
170
    def test_default_values(self):
 
171
        """ default values """
 
172
 
 
173
        p = self.preferences
 
174
 
 
175
        class AcmeUIPreferencesHelper(PreferencesHelper):
 
176
            """ A helper! """
 
177
 
 
178
            # The path to the preferences node that contains our preferences.
 
179
            preferences_path = 'acme.ui'
 
180
 
 
181
            # The traits that we want to initialize from preferences.
 
182
            bgcolor     = Str('blue')
 
183
            width       = Int(50)
 
184
            ratio       = Float(1.0)
 
185
            visible     = Bool(True)
 
186
            description = Unicode(u'description')
 
187
            offsets     = List(Int, [1, 2, 3, 4])
 
188
            names       = List(Str, ['joe', 'fred', 'jane'])
 
189
 
 
190
        helper = AcmeUIPreferencesHelper()
 
191
 
 
192
        # Make sure the helper was initialized properly.
 
193
        self.assertEqual('blue', helper.bgcolor)
 
194
        self.assertEqual(50, helper.width)
 
195
        self.assertEqual(1.0, helper.ratio)
 
196
        self.assertEqual(True, helper.visible)
 
197
        self.assertEqual(u'description', helper.description)
 
198
        self.assertEqual([1, 2, 3, 4], helper.offsets)
 
199
        self.assertEqual(['joe', 'fred', 'jane'], helper.names)
 
200
 
 
201
        return
 
202
 
 
203
    def test_no_preferences_path(self):
 
204
        """ no preferences path """
 
205
 
 
206
        p = self.preferences
 
207
        p.load(self.example)
 
208
 
 
209
        class AcmeUIPreferencesHelper(PreferencesHelper):
 
210
            """ A helper! """
 
211
 
 
212
            # The traits that we want to initialize from preferences.
 
213
            bgcolor     = Str
 
214
            width       = Int
 
215
            ratio       = Float
 
216
            visible     = Bool
 
217
            description = Unicode
 
218
            offsets     = List(Int)
 
219
            names       = List(Str)
 
220
 
 
221
        # Cannot create a helper with a preferences path.
 
222
        self.failUnlessRaises(SystemError, AcmeUIPreferencesHelper)
 
223
 
 
224
        return
 
225
 
 
226
    def test_sync_trait(self):
 
227
        """ sync trait """
 
228
 
 
229
        class Widget(HasTraits):
 
230
            """ A widget! """
 
231
 
 
232
            background_color = Str
 
233
 
 
234
        w = Widget()
 
235
        w.on_trait_change(listener)
 
236
 
 
237
        p = self.preferences
 
238
        p.load(self.example)
 
239
 
 
240
        class AcmeUIPreferencesHelper(PreferencesHelper):
 
241
            """ A helper! """
 
242
 
 
243
            # The path to the preferences node that contains our preferences.
 
244
            preferences_path = 'acme.ui'
 
245
 
 
246
            # The traits that we want to initialize from preferences.
 
247
            bgcolor     = Str
 
248
            width       = Int
 
249
            ratio       = Float
 
250
            visible     = Bool
 
251
            description = Unicode
 
252
            offsets     = List(Int)
 
253
            names       = List(Str)
 
254
 
 
255
        helper = AcmeUIPreferencesHelper()
 
256
        helper.sync_trait('bgcolor', w, 'background_color')
 
257
 
 
258
        # Make sure the helper was initialized properly.
 
259
        self.assertEqual('blue', helper.bgcolor)
 
260
        self.assertEqual(50, helper.width)
 
261
        self.assertEqual(1.0, helper.ratio)
 
262
        self.assertEqual(True, helper.visible)
 
263
        self.assertEqual(u'acme ui', helper.description)
 
264
        self.assertEqual([1, 2, 3, 4], helper.offsets)
 
265
        self.assertEqual(['joe', 'fred', 'jane'], helper.names)
 
266
 
 
267
        self.assertEqual('blue', w.background_color)
 
268
 
 
269
        # Make sure we can set the preference via the helper...
 
270
        helper.bgcolor = 'yellow'
 
271
        self.assertEqual('yellow', p.get('acme.ui.bgcolor'))
 
272
        self.assertEqual('yellow', helper.bgcolor)
 
273
 
 
274
        self.assertEqual('yellow', w.background_color)
 
275
 
 
276
        # ... and that the correct trait change event was fired.
 
277
        self.assertEqual(w, listener.obj)
 
278
        self.assertEqual('background_color', listener.trait_name)
 
279
        self.assertEqual('blue', listener.old)
 
280
        self.assertEqual('yellow', listener.new)
 
281
 
 
282
        # Make sure we can set the preference via the preferences node...
 
283
        p.set('acme.ui.bgcolor', 'red')
 
284
        self.assertEqual('red', p.get('acme.ui.bgcolor'))
 
285
        self.assertEqual('red', helper.bgcolor)
 
286
 
 
287
        self.assertEqual('red', w.background_color)
 
288
 
 
289
        # ... and that the correct trait change event was fired.
 
290
        self.assertEqual(w, listener.obj)
 
291
        self.assertEqual('background_color', listener.trait_name)
 
292
        self.assertEqual('yellow', listener.old)
 
293
        self.assertEqual('red', listener.new)
 
294
 
 
295
        return
 
296
 
 
297
    def test_scoped_preferences(self):
 
298
        """ scoped preferences """
 
299
 
 
300
        p = set_default_preferences(ScopedPreferences())
 
301
 
 
302
        # Set a preference value in the default scope.
 
303
        p.set('default/acme.ui.bgcolor', 'blue')
 
304
 
 
305
        class AcmeUIPreferencesHelper(PreferencesHelper):
 
306
            """ A helper! """
 
307
 
 
308
            # The path to the preferences node that contains our preferences.
 
309
            preferences_path = 'acme.ui'
 
310
 
 
311
            # The traits that we want to initialize from preferences.
 
312
            bgcolor = Str
 
313
 
 
314
            # A trait for a preference that does not exist yet.
 
315
            name = Str
 
316
 
 
317
        helper = AcmeUIPreferencesHelper()
 
318
 
 
319
        # Make sure the trait is set!
 
320
        self.assertEqual('blue', helper.bgcolor)
 
321
 
 
322
        # And that the non-existent trait gets the default value.
 
323
        self.assertEqual('', helper.name)
 
324
 
 
325
        return
 
326
 
 
327
    def test_preference_not_in_file(self):
 
328
        """ preference not in file """
 
329
 
 
330
        class AcmeUIPreferencesHelper(PreferencesHelper):
 
331
            """ A helper! """
 
332
 
 
333
            # The path to the preferences node that contains our preferences.
 
334
            preferences_path = 'acme.ui'
 
335
 
 
336
            # A trait that has no corresponding value in the file.
 
337
            title = Str('Acme')
 
338
 
 
339
        helper = AcmeUIPreferencesHelper()
 
340
 
 
341
        # Make sure the trait is set!
 
342
        self.assertEqual('Acme', helper.title)
 
343
 
 
344
        # Set a new value.
 
345
        helper.title = 'Acme Plus'
 
346
 
 
347
        # Make sure the trait is set!
 
348
        self.assertEqual('Acme Plus', helper.title)
 
349
        self.assertEqual('Acme Plus', self.preferences.get('acme.ui.title'))
 
350
 
 
351
        return
 
352
 
 
353
    def test_preferences_node_changed(self):
 
354
        """ preferences node changed """
 
355
 
 
356
        p = self.preferences
 
357
        p.load(self.example)
 
358
 
 
359
        class AcmeUIPreferencesHelper(PreferencesHelper):
 
360
            """ A helper! """
 
361
 
 
362
            # The path to the preferences node that contains our preferences.
 
363
            preferences_path = 'acme.ui'
 
364
 
 
365
            # The traits that we want to initialize from preferences.
 
366
            bgcolor     = Str
 
367
            width       = Int
 
368
            ratio       = Float
 
369
            visible     = Bool
 
370
            description = Unicode
 
371
            offsets     = List(Int)
 
372
            names       = List(Str)
 
373
 
 
374
        helper = AcmeUIPreferencesHelper()
 
375
 
 
376
        # We only listen to some of the traits so the testing is easier.
 
377
        helper.on_trait_change(listener, ['bgcolor', 'width'])
 
378
 
 
379
        # Create a new preference node.
 
380
        p1 = Preferences()
 
381
        p1.load(self.example)
 
382
        p1.set('acme.ui.bgcolor', 'red')
 
383
        p1.set('acme.ui.width', 40)
 
384
 
 
385
        # Set the new preferences
 
386
        helper.preferences = p1
 
387
 
 
388
        # Test event handling.
 
389
        self.assertEqual(helper, listener.obj)
 
390
        self.assertEqual('width', listener.trait_name)
 
391
        self.assertEqual(50, listener.old)
 
392
        self.assertEqual(40, listener.new)
 
393
 
 
394
        # Test re-initialization.
 
395
        self.assertEqual(helper.bgcolor, 'red')
 
396
        self.assertEqual(helper.width, 40)
 
397
 
 
398
        # Test event handling.
 
399
        p1.set('acme.ui.bgcolor', 'black')
 
400
        self.assertEqual(helper, listener.obj)
 
401
        self.assertEqual('bgcolor', listener.trait_name)
 
402
        self.assertEqual('red', listener.old)
 
403
        self.assertEqual('black', listener.new)
 
404
 
 
405
        # This should not trigger any new changes since we are setting values
 
406
        # on the old preferences node.
 
407
        p.set('acme.ui.bgcolor', 'white')
 
408
        self.assertEqual(helper, listener.obj)
 
409
        self.assertEqual('bgcolor', listener.trait_name)
 
410
        self.assertEqual('red', listener.old)
 
411
        self.assertEqual('black', listener.new)
 
412
 
 
413
        return
 
414
 
 
415
    def test_nested_set_in_trait_change_handler(self):
 
416
        """ nested set in trait change handler """
 
417
 
 
418
        p = self.preferences
 
419
        p.load(self.example)
 
420
 
 
421
        class AcmeUIPreferencesHelper(PreferencesHelper):
 
422
            """ A helper! """
 
423
 
 
424
            # The traits that we want to initialize from preferences.
 
425
            bgcolor     = Str
 
426
            width       = Int
 
427
            ratio       = Float
 
428
            visible     = Bool
 
429
            description = Unicode
 
430
            offsets     = List(Int)
 
431
            names       = List(Str)
 
432
 
 
433
            # When the width changes, change the ratio.
 
434
            def _width_changed(self, trait_name, old, new):
 
435
                """ Static trait change handler. """
 
436
 
 
437
                self.ratio = 3.0
 
438
 
 
439
                return
 
440
 
 
441
        helper = AcmeUIPreferencesHelper(preferences_path='acme.ui')
 
442
 
 
443
        # Make sure the helper was initialized properly.
 
444
        self.assertEqual('blue', helper.bgcolor)
 
445
        self.assertEqual(50, helper.width)
 
446
        self.assertEqual(1.0, helper.ratio)
 
447
        self.assertEqual(True, helper.visible)
 
448
        self.assertEqual(u'acme ui', helper.description)
 
449
        self.assertEqual([1, 2, 3, 4], helper.offsets)
 
450
        self.assertEqual(['joe', 'fred', 'jane'], helper.names)
 
451
 
 
452
        # Change the width via the preferences node. This should cause the
 
453
        # ratio to get set via the static trait change handler on the helper.
 
454
        p.set('acme.ui.width', 42)
 
455
        self.assertEqual(42, helper.width)
 
456
        self.assertEqual('42', p.get('acme.ui.width'))
 
457
 
 
458
        # Did the ratio get changed?
 
459
        self.assertEqual(3.0, helper.ratio)
 
460
        self.assertEqual('3.0', p.get('acme.ui.ratio'))
 
461
 
 
462
        return
 
463
 
 
464
    # fixme: No comments - nice work... I added the doc string and the 'return'
 
465
    # to be compatible with the rest of the module. Interns please note correct
 
466
    # procedure when modifying existing code. If in doubt, ask a developer.
 
467
    def test_unevaluated_strings(self):
 
468
        """ unevaluated strings """
 
469
 
 
470
        p = self.preferences
 
471
        p.load(self.example)
 
472
 
 
473
        class AcmeUIPreferencesHelper(PreferencesHelper):
 
474
            width = Any(is_str=True)
 
475
 
 
476
        helper = AcmeUIPreferencesHelper(preferences_path='acme.ui')
 
477
 
 
478
        self.assertEqual('50', helper.width)
 
479
 
 
480
        return
 
481
 
 
482
 
 
483
# Entry point for stand-alone testing.
 
484
if __name__ == '__main__':
 
485
    unittest.main()
 
486
 
 
487
#### EOF ######################################################################