44
55"""
66# Author: Prabhu Ramachandran
7- # Copyright (c) 2004-2015 , Enthought, Inc.
7+ # Copyright (c) 2004-2018 , Enthought, Inc.
88# License: BSD Style.
99
1010import unittest
2323# testing.
2424class Prop (tvtk_base .TVTKBase ):
2525 def __init__ (self , obj = None , update = 1 , ** traits ):
26- tvtk_base .TVTKBase .__init__ (self , vtk .vtkProperty , obj , update , ** traits )
26+ tvtk_base .TVTKBase .__init__ (
27+ self , vtk .vtkProperty , obj , update , ** traits
28+ )
2729
2830 edge_visibility = tvtk_base .false_bool_trait
31+
2932 def _edge_visibility_changed (self , old_val , new_val ):
3033 self ._do_change (self ._vtk_obj .SetEdgeVisibility , self .edge_visibility_ )
3134
32- representation = traits .Trait ('surface' ,
33- tvtk_base .TraitRevPrefixMap ({'points' : 0 , 'wireframe' : 1 , 'surface' : 2 }))
35+ representation = traits .Trait (
36+ 'surface' ,
37+ tvtk_base .TraitRevPrefixMap (
38+ {'points' : 0 , 'wireframe' : 1 , 'surface' : 2 })
39+ )
40+
3441 def _representation_changed (self , old_val , new_val ):
3542 self ._do_change (self ._vtk_obj .SetRepresentation , self .representation_ )
3643
3744 opacity = traits .Trait (1.0 , traits .Range (0.0 , 1.0 ))
45+
3846 def _opacity_changed (self , old_val , new_val ):
3947 self ._do_change (self ._vtk_obj .SetOpacity ,
4048 self .opacity )
4149
4250 specular_color = tvtk_base .vtk_color_trait ((1.0 , 1.0 , 1.0 ))
51+
4352 def _specular_color_changed (self , old_val , new_val ):
4453 self ._do_change (self ._vtk_obj .SetSpecularColor ,
4554 self .specular_color , 1 )
4655
4756 diffuse_color = tvtk_base .vtk_color_trait ((1.0 , 1.0 , 1.0 ))
57+
4858 def _diffuse_color_changed (self , old_val , new_val ):
4959 self ._do_change (self ._vtk_obj .SetDiffuseColor ,
5060 self .diffuse_color , 1 )
5161
5262 color = tvtk_base .vtk_color_trait ((1.0 , 1.0 , 1.0 ))
63+
5364 def _color_changed (self , old_val , new_val ):
5465 self ._do_change (self ._vtk_obj .SetColor ,
5566 self .color )
@@ -62,7 +73,6 @@ def _color_changed(self, old_val, new_val):
6273 ('representation' , 'GetRepresentation' ))
6374
6475
65-
6676class TestTVTKBase (unittest .TestCase ):
6777 def test_tvtk_name (self ):
6878 """Test VTK to TVTK class name conversion."""
@@ -77,9 +87,9 @@ def test_tvtk_name(self):
7787 num = ['Zero' , 'One' , 'Two' , 'Three' , 'Four' , 'Five' ,
7888 'Six' , 'Seven' , 'Eight' , 'Nine' ]
7989 for i in range (10 ):
80- vn = 'vtk%dA' % i
90+ vn = 'vtk%dA' % i
8191 tn = get_tvtk_name (vn )
82- self .assertEqual (tn , '%sA' % num [i ])
92+ self .assertEqual (tn , '%sA' % num [i ])
8393
8494 def test_camel2enthought (self ):
8595 """Test CamelCase to Enthought style name conversion."""
@@ -103,16 +113,31 @@ def test_do_change(self):
103113 p .edge_visibility = not p .edge_visibility
104114 p .representation = 'p'
105115 p .opacity = 0.5
106- p .color = (0 ,1 , 0 )
107- p .diffuse_color = (1 ,1 , 1 )
108- p .specular_color = (1 ,1 , 0 )
116+ p .color = (0 , 1 , 0 )
117+ p .diffuse_color = (1 , 1 , 1 )
118+ p .specular_color = (1 , 1 , 0 )
109119 for t , g in p ._updateable_traits_ :
110120 val = getattr (p ._vtk_obj , g )()
111121 if t == 'representation' :
112122 self .assertEqual (val , getattr (p , t + '_' ))
113123 else :
114124 self .assertEqual (val , getattr (p , t ))
115125
126+ def test_wrap_call_is_graceful_on_failure (self ):
127+ # Given
128+ p = Prop ()
129+
130+ # When
131+ try :
132+ # Make a mistake
133+ p ._wrap_call (p ._vtk_obj .SetLineWidth , 'a' )
134+ except TypeError :
135+ pass
136+
137+ # Then
138+ # The _in_set should be reset to zero.
139+ self .assertEqual (p ._in_set , 0 )
140+
116141 def test_auto_update (self ):
117142 """Test trait updation when the VTK object changes."""
118143 p = Prop ()
@@ -176,9 +201,9 @@ def test_pickle(self):
176201 p .edge_visibility = 1
177202 p .representation = 'p'
178203 p .opacity = 0.5
179- p .color = (0 ,1 , 0 )
180- p .diffuse_color = (0 ,1 , 1 )
181- p .specular_color = (1 ,1 , 0 )
204+ p .color = (0 , 1 , 0 )
205+ p .diffuse_color = (0 , 1 , 1 )
206+ p .specular_color = (1 , 1 , 0 )
182207
183208 s = pickle .dumps (p )
184209 del p
@@ -218,9 +243,9 @@ def test_rev_prefix_map(self):
218243 p .representation = 'points'
219244 p .representation = 2
220245 self .assertEqual (p .representation , 'surface' )
221- self .assertRaises (traits .TraitError , setattr , p ,
246+ self .assertRaises (traits .TraitError , setattr , p ,
222247 'representation' , 'points1' )
223- self .assertRaises (traits .TraitError , setattr , p ,
248+ self .assertRaises (traits .TraitError , setattr , p ,
224249 'representation' , 'POINTS' )
225250
226251 def test_deref_vtk (self ):
@@ -242,6 +267,25 @@ def test_obj_del(self):
242267 del p
243268 self .assertEqual (ref (), None )
244269
270+ def test_global_disable_update (self ):
271+ # Given
272+ p = Prop ()
273+ vp = tvtk_base .deref_vtk (p )
274+
275+ # When
276+ with tvtk_base .global_disable_update ():
277+ vp .SetOpacity (0.5 )
278+ vp .Modified ()
279+
280+ # Then
281+ self .assertEqual (p .opacity , 1.0 )
282+
283+ # When
284+ vp .SetOpacity (0.4 )
285+
286+ # Then
287+ self .assertEqual (p .opacity , 0.4 )
288+
245289 def test_strict_traits (self ):
246290 """Test if TVTK objects use strict traits."""
247291 p = Prop ()
@@ -251,7 +295,7 @@ def test_strict_traits(self):
251295
252296 def test_init_traits (self ):
253297 """Test if the objects traits can be set in __init__."""
254- p = Prop (opacity = 0.1 , color = (1 ,0 , 0 ), representation = 'p' )
298+ p = Prop (opacity = 0.1 , color = (1 , 0 , 0 ), representation = 'p' )
255299 self .assertEqual (p .opacity , 0.1 )
256300 self .assertEqual (p .color , (1.0 , 0.0 , 0.0 ))
257301 self .assertEqual (p .representation , 'points' )
@@ -272,7 +316,7 @@ def test_zz_object_cache(self):
272316 self .assertEqual (p , tvtk_base ._object_cache .get (addr ))
273317
274318 del p
275- gc .collect () # Force collection.
319+ gc .collect () # Force collection.
276320 self .assertEqual (l1 , len (tvtk_base ._object_cache ))
277321 self .assertEqual (addr in tvtk_base ._object_cache , False )
278322
0 commit comments