# Functions used by widget tests.
if Tkinter
.TkVersion
>= 8.4:
script_name
= imp
.find_module(__name__
)[1]
if not os
.path
.isabs(script_name
):
script_name
= os
.path
.join(os
.getcwd(), script_name
)
script_dir
= os
.path
.dirname(script_name
)
if not os
.path
.islink(script_name
):
script_name
= os
.path
.join(script_dir
, os
.readlink(script_name
))
script_dir
= os
.path
.join(os
.getcwd(), script_dir
)
script_dir
= os
.path
.normpath(script_dir
)
# Add the '../../..' directory to the path.
package_dir
= os
.path
.dirname(script_dir
)
package_dir
= os
.path
.dirname(package_dir
)
package_dir
= os
.path
.dirname(package_dir
)
sys
.path
[:0] = [package_dir
]
# Need to import TestVersion, rather than do its work here, since
# __name__ will be known there.
# Set this to 1 to generate tracebacks on exceptions, rather than
# catching them and continuing with the tests.
# This is useful for debugging the test scripts.
##############################################################################
rand
= (rand
* 125) % 2796203
global _initialised
, font
, flagup
, earthris
, emptyimage
, \
stringvar
, floatvar
, root
, reliefs
root
= Tkinter
.Tk(className
= 'PmwTest')
Pmw
.initialise(root
, size
= size
, fontScheme
= 'pmw2')
font
['variable'] = '-Adobe-Helvetica-Bold-R-Normal--*-120-*-*-*-*-*-*'
flagup
= Tkinter
.BitmapImage(file = 'flagup.bmp')
earthris
= Tkinter
.PhotoImage(file = 'earthris.gif')
emptyimage
= Tkinter
.PhotoImage()
stringvar
= Tkinter
.StringVar()
stringvar
.set('this is some text')
floatvar
= Tkinter
.DoubleVar()
global vectorSize
, vector_x
, vector_y
vector_x
= Pmw
.Blt
.Vector()
vector_y
.append(Pmw
.Blt
.Vector())
for index
in range(vectorSize
):
vector_y
[0].append(random() % 100)
vector_y
[1].append(random() % 200)
vector_y
[2].append(random() % 100 + 300)
# "solid" relief type was added to 8.0
if Tkinter
.TkVersion
>= 8.0:
reliefs
= 'flat, groove, raised, ridge, solid, or sunken'
reliefs
= 'flat, groove, raised, ridge, or sunken'
return Pmw
.Blt
.haveblt(root
)
def setverbose(newverbose
):
def printtraceback(newprintTraceback
= 1):
_printTraceback
= newprintTraceback
return len(widget
.configure())
# Callback taking 1 argument
def callback2(dummy1
, dummy2
):
# Callback taking 2 arguments
# Callback taking zero or more arguments
def set_geom(width
, height
):
_currentToplevel
.geometry(str(width
) + 'x' + str(height
))
def runTests(allTestData
):
root
.after(_delay
, _runTest
, None, None, allTestData
, 0, -1, -1)
##############################################################################
def _print_results(result
, expected
, description
):
if type(expected
) == types
.ClassType
:
if hasattr(result
, '__class__'):
ok
= (result
.__class
__ == expected
)
ok
= (result
== expected
)
# Megawidgets return a value of the correct type. Tk widgets
# always return a string, so convert the string and check again.
if type(expected
) == types
.InstanceType
:
if result
== str(expected
) and (
expected
is earthris
or expected
is stringvar
or
expected
is floatvar
or expected
is flagup
):
elif hasattr(_tkinter
, 'Tcl_Obj') and \
type(result
) == _tkinter
.Tcl_Obj
:
ok
= (str(stringvar
) == result
.string
)
elif type(expected
) == types
.IntType
:
if type(result
) is types
.StringType
:
ok
= (string
.atoi(result
) == expected
)
elif hasattr(_tkinter
, 'Tcl_Obj') and \
type(result
) == _tkinter
.Tcl_Obj
:
ok
= (string
.atoi(str(result
)) == expected
)
elif type(expected
) == types
.FloatType
:
if type(result
) is types
.StringType
:
ok
= (string
.atof(result
) == expected
)
elif expected
== callback
:
ok
= re
.search('^[0-9]*callback$', str(result
)) is not None
elif expected
== callback1
:
ok
= re
.search('^[0-9]*callback1$', str(result
)) is not None
elif expected
== callback2
:
ok
= re
.search('^[0-9]*callback2$', str(result
)) is not None
elif expected
== actioncallback
:
ok
= re
.search('^[0-9]*actioncallback$',str(result
)) is not None
if not ok
or _verbose
> 0:
print '====', description
if not ok
or _verbose
> 1:
print result
, type(result
)
print '---- result should have been:'
print expected
, type(expected
)
def _destroyToplevel(top
, title
):
print '==== destruction of Toplevel for', title
print '==== construction of Toplevel for', title
def _constructor(isWidget
, top
, classCmd
, kw
):
print '====', classCmd
.__name
__, 'construction'
w
= apply(classCmd
, (top
,), kw
)
w
= apply(classCmd
, (top
,), kw
)
print 'Could not construct', classCmd
.__name
__
isMegaWidget
= hasattr(classCmd
, 'defineoptions')
# Check the option types:
option_list
= options
.keys()
for option
in option_list
:
# Some of the options (like bd, bg and fg) have only two parts
# and are just abbreviations. Only check 'real' options.
if len(options
[option
]) == 5:
initoption
= isMegaWidget
and w
.isinitoption(option
)
if option
not in ('class', 'container') and not initoption
:
apply(w
.configure
, (), {option
: value
})
newvalue
= w
.cget(option
)
print '====', classCmd
.__name
__, 'widget', \
'\'' + option
+ '\'', 'option'
print '---- setting option returns different value'
print '==== new value was:'
print newvalue
, type(newvalue
)
print '---- set value was:'
if option
not in ('class', 'container') and not initoption
:
apply(w
.configure
, (), {option
: value
})
newvalue
= w
.cget(option
)
if hasattr(_tkinter
, 'Tcl_Obj') and \
(type(newvalue
) == _tkinter
.Tcl_Obj
and str(newvalue
) != str(value
))
(type(newvalue
) != _tkinter
.Tcl_Obj
not hasattr(_tkinter
, 'Tcl_Obj') and
print '====', classCmd
.__name
__, 'widget', \
'\'' + option
+ '\'', 'option'
print '---- setting option returns different value'
print '==== new value was:'
print `newvalue`
, type(newvalue
)
print '---- set value was:'
print `value`
, type(value
)
print '====', classCmd
.__name
__, 'widget', \
'\'' + option
+ '\'', 'option'
print '---- could not set option'
print '====', classCmd
.__name
__, 'widget', \
'\'' + option
+ '\'', 'option'
print '---- unknown option'
if hasattr(classCmd
, 'geometry'):
w
.title(classCmd
.__name
__)
w
= apply(classCmd
, (), kw
)
def _destructor(widget
, isWidget
):
print '====', widget
.__class
__.__name
__, 'destruction'
ref
= sys
.getrefcount(widget
)
if ref
!= refcountafterdestroy
:
print '====', widget
.__class
__.__name
__, 'destructor'
print '---- refcount', ref
, 'not zero after destruction'
print 'Could not destroy', widget
.__class
__.__name
__
# Structure of allTestData:
def _runTest(top
, w
, allTestData
, index0
, index1
, index2
):
if index0
>= len(allTestData
):
classCmd
, fileTests
= allTestData
[index0
]
if classCmd
== Tkinter
.Menu
:
isToplevel
= hasattr(classCmd
, 'userdeletefunc')
isWidget
= hasattr(classCmd
, 'cget')
title
= classCmd
.__name
__
elif index1
>= len(fileTests
):
_destroyToplevel(top
, title
)
methodTests
, kw
= fileTests
[index1
]
w
= _constructor(isWidget
, top
, classCmd
, kw
)
elif index2
>= len(methodTests
):
if _destructor(w
, isWidget
) is None:
methodTestData
= methodTests
[index2
]
if type(methodTestData
[0]) == types
.StringType
:
_configureTest(w
, methodTestData
)
_methodTest(w
, methodTestData
)
root
.after(_delay
, _runTest
, top
, w
, allTestData
, index0
, index1
, index2
)
def _configureTest(w
, testData
):
apply(w
.configure
, (), {option
: value
})
apply(w
.configure
, (), {option
: value
})
result
= _getErrorValue()
_print_results(result
, expected
, \
w
.__class
__.__name
__ + ' option ' + str(testData
))
exc_type
, exc_value
, exc_traceback
= sys
.exc_info()
if type(exc_type
) == types
.ClassType
:
# Handle python 1.5 class exceptions.
exc_type
= exc_type
.__name
__
if type(exc_value
) == types
.StringType
:
return exc_type
+ ': ' + exc_value
exc_value_str
= str(exc_value
)
if exc_value_str
[:1] == "'" and exc_value_str
[-1:] == "'":
exc_value_str
= exc_value_str
[1:-1]
return exc_type
+ ': ' + exc_value_str
def _methodTest(w
, testData
):
if type(testData
[2]) == types
.DictionaryType
:
if type(args
) != types
.TupleType
:
if type(func
) == types
.MethodType
and func
.im_self
is None:
result
= apply(func
, args
, kw
)
result
= apply(func
, args
, kw
)
result
= _getErrorValue()
if hasattr(func
, 'im_func'):
name
= w
.__class
__.__name
__ + ' method ' + \
func
.im_func
.func_code
.co_name
name
= 'function ' + func
.__name
__
name
= name
+ ' ' + str(origArgs
)
name
= name
+ ' ' + str(kw
)
_print_results(result
, expected
, name
)