1
""" Tests for the preferences helper. """
4
# Standard library imports.
7
# Major package imports.
8
from pkg_resources import resource_filename
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
18
def listener(obj, trait_name, old, new):
19
""" A useful trait change handler for testing! """
22
listener.trait_name = trait_name
29
# This module's package.
30
PKG = 'apptools.preferences.tests'
33
class PreferencesHelperTestCase(unittest.TestCase):
34
""" Tests for the preferences helper. """
36
###########################################################################
37
# 'TestCase' interface.
38
###########################################################################
41
""" Prepares the test fixture before each test method is called. """
43
self.preferences = set_default_preferences(Preferences())
45
# The filename of the example preferences file.
46
self.example = resource_filename(PKG, 'example.ini')
51
""" Called immediately after each test method has been called. """
55
###########################################################################
57
###########################################################################
59
def test_class_scope_preferences_path(self):
60
""" class scope preferences path """
65
class AcmeUIPreferencesHelper(PreferencesHelper):
68
# The path to the preferences node that contains our preferences.
69
preferences_path = 'acme.ui'
71
# The traits that we want to initialize from preferences.
80
helper = AcmeUIPreferencesHelper()
81
helper.on_trait_change(listener)
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)
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)
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)
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)
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)
116
def test_instance_scope_preferences_path(self):
117
""" instance scope preferences path """
122
class AcmeUIPreferencesHelper(PreferencesHelper):
125
# The traits that we want to initialize from preferences.
130
description = Unicode
134
helper = AcmeUIPreferencesHelper(preferences_path='acme.ui')
135
helper.on_trait_change(listener)
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)
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)
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)
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)
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)
170
def test_default_values(self):
171
""" default values """
175
class AcmeUIPreferencesHelper(PreferencesHelper):
178
# The path to the preferences node that contains our preferences.
179
preferences_path = 'acme.ui'
181
# The traits that we want to initialize from preferences.
182
bgcolor = Str('blue')
186
description = Unicode(u'description')
187
offsets = List(Int, [1, 2, 3, 4])
188
names = List(Str, ['joe', 'fred', 'jane'])
190
helper = AcmeUIPreferencesHelper()
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)
203
def test_no_preferences_path(self):
204
""" no preferences path """
209
class AcmeUIPreferencesHelper(PreferencesHelper):
212
# The traits that we want to initialize from preferences.
217
description = Unicode
221
# Cannot create a helper with a preferences path.
222
self.failUnlessRaises(SystemError, AcmeUIPreferencesHelper)
226
def test_sync_trait(self):
229
class Widget(HasTraits):
232
background_color = Str
235
w.on_trait_change(listener)
240
class AcmeUIPreferencesHelper(PreferencesHelper):
243
# The path to the preferences node that contains our preferences.
244
preferences_path = 'acme.ui'
246
# The traits that we want to initialize from preferences.
251
description = Unicode
255
helper = AcmeUIPreferencesHelper()
256
helper.sync_trait('bgcolor', w, 'background_color')
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)
267
self.assertEqual('blue', w.background_color)
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)
274
self.assertEqual('yellow', w.background_color)
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)
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)
287
self.assertEqual('red', w.background_color)
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)
297
def test_scoped_preferences(self):
298
""" scoped preferences """
300
p = set_default_preferences(ScopedPreferences())
302
# Set a preference value in the default scope.
303
p.set('default/acme.ui.bgcolor', 'blue')
305
class AcmeUIPreferencesHelper(PreferencesHelper):
308
# The path to the preferences node that contains our preferences.
309
preferences_path = 'acme.ui'
311
# The traits that we want to initialize from preferences.
314
# A trait for a preference that does not exist yet.
317
helper = AcmeUIPreferencesHelper()
319
# Make sure the trait is set!
320
self.assertEqual('blue', helper.bgcolor)
322
# And that the non-existent trait gets the default value.
323
self.assertEqual('', helper.name)
327
def test_preference_not_in_file(self):
328
""" preference not in file """
330
class AcmeUIPreferencesHelper(PreferencesHelper):
333
# The path to the preferences node that contains our preferences.
334
preferences_path = 'acme.ui'
336
# A trait that has no corresponding value in the file.
339
helper = AcmeUIPreferencesHelper()
341
# Make sure the trait is set!
342
self.assertEqual('Acme', helper.title)
345
helper.title = 'Acme Plus'
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'))
353
def test_preferences_node_changed(self):
354
""" preferences node changed """
359
class AcmeUIPreferencesHelper(PreferencesHelper):
362
# The path to the preferences node that contains our preferences.
363
preferences_path = 'acme.ui'
365
# The traits that we want to initialize from preferences.
370
description = Unicode
374
helper = AcmeUIPreferencesHelper()
376
# We only listen to some of the traits so the testing is easier.
377
helper.on_trait_change(listener, ['bgcolor', 'width'])
379
# Create a new preference node.
381
p1.load(self.example)
382
p1.set('acme.ui.bgcolor', 'red')
383
p1.set('acme.ui.width', 40)
385
# Set the new preferences
386
helper.preferences = p1
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)
394
# Test re-initialization.
395
self.assertEqual(helper.bgcolor, 'red')
396
self.assertEqual(helper.width, 40)
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)
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)
415
def test_nested_set_in_trait_change_handler(self):
416
""" nested set in trait change handler """
421
class AcmeUIPreferencesHelper(PreferencesHelper):
424
# The traits that we want to initialize from preferences.
429
description = Unicode
433
# When the width changes, change the ratio.
434
def _width_changed(self, trait_name, old, new):
435
""" Static trait change handler. """
441
helper = AcmeUIPreferencesHelper(preferences_path='acme.ui')
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)
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'))
458
# Did the ratio get changed?
459
self.assertEqual(3.0, helper.ratio)
460
self.assertEqual('3.0', p.get('acme.ui.ratio'))
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 """
473
class AcmeUIPreferencesHelper(PreferencesHelper):
474
width = Any(is_str=True)
476
helper = AcmeUIPreferencesHelper(preferences_path='acme.ui')
478
self.assertEqual('50', helper.width)
483
# Entry point for stand-alone testing.
484
if __name__ == '__main__':
487
#### EOF ######################################################################