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
|
#
# Tests for [incr Tk] widgets based on itk::Widget
# ----------------------------------------------------------------------
# AUTHOR: Michael J. McLennan
# Bell Labs Innovations for Lucent Technologies
# mmclennan@lucent.com
# http://www.tcltk.com/itcl
#
# RCS: $Id: widget.test,v 1.6 2004/09/22 09:37:09 davygrvy Exp $
# ----------------------------------------------------------------------
# Copyright (c) 1993-1998 Lucent Technologies, Inc.
# ======================================================================
# See the file "license.terms" for information on usage and
# redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
package require tcltest
namespace import -force ::tcltest::*
::tcltest::loadTestedCommands
# ----------------------------------------------------------------------
# Simple mega-widget
# ----------------------------------------------------------------------
test widget-1.1 {define a simple mega-widget class} {
option add *TestWidget.background linen
option add *TestWidget.borderWidth 2
option add *TestWidget.command ""
option add *TestWidget.cursor ""
option add *TestWidget.foreground navy
option add *TestWidget.highlight white
option add *TestWidget.normal ivory
option add *TestWidget.text ""
itcl::class TestWidget {
inherit itk::Widget
constructor {args} {
itk_component add test1 {
label $itk_interior.t1
} {
keep -background -foreground -cursor
keep -text
}
pack $itk_component(test1) -side left -padx 2
itk_component add test2 {
button $itk_interior.t2 -text "Push Me"
} {
keep -foreground -cursor -borderwidth -command
rename -background -normal normal Background
rename -activebackground -highlight highlight Foreground
}
pack $itk_component(test2) -side right -fill x -pady 2
eval itk_initialize $args
}
private variable status ""
public method action {info} {
lappend status $info
}
public method do {cmd} {
eval $cmd
}
itk_option define -status status Status {} {
lappend status $itk_option(-status)
}
}
TestWidget .#auto
} {.testWidget0}
pack .testWidget0
test widget-1.2 {check the list of configuration options} {
.testWidget0 configure
} {{-background background Background linen linen} {-borderwidth borderWidth BorderWidth 2 2} {-clientdata clientData ClientData {} {}} {-command command Command {} {}} {-cursor cursor Cursor {} {}} {-foreground foreground Foreground navy navy} {-highlight highlight Foreground white white} {-normal normal Background ivory ivory} {-status status Status {} {}} {-text text Text {} {}}}
set unique 0
foreach test {
{-background {-background background Background linen linen}}
{-borderwidth {-borderwidth borderWidth BorderWidth 2 2}}
{-clientdata {-clientdata clientData ClientData {} {}}}
{-command {-command command Command {} {}}}
{-cursor {-cursor cursor Cursor {} {}}}
{-foreground {-foreground foreground Foreground navy navy}}
{-highlight {-highlight highlight Foreground white white}}
{-normal {-normal normal Background ivory ivory}}
{-status {-status status Status {} {}}}
{-text {-text text Text {} {}}}
} {
set opt [lindex $test 0]
set result [lindex $test 1]
test widget-1.3.[incr unique] {check individual configuration options} {
.testWidget0 configure $opt
} $result
}
set unique 0
foreach test {
{-background red}
{-borderwidth 1}
{-clientdata "foo bar"}
{-command {puts "hello!"}}
{-cursor trek}
{-foreground IndianRed}
{-highlight MistyRose}
{-normal MistyRose2}
{-status "test message"}
{-text "Label:"}
} {
set opt [lindex $test 0]
set value [lindex $test 1]
test widget-1.4.[incr unique] {set individual configuration options} {
list [.testWidget0 configure $opt $value] \
[.testWidget0 cget $opt] \
[.testWidget0 do "set itk_option($opt)"]
} [list "" $value $value]
}
test widget-1.5 {check the list components} {
lsort [.testWidget0 component]
} {hull test1 test2}
set unique 0
foreach test {
{hull .testWidget0}
{test1 .testWidget0.t1}
{test2 .testWidget0.t2}
} {
set name [lindex $test 0]
set win [lindex $test 1]
test widget-1.6 {check the window for each component} {
list [.testWidget0 component $name] \
[.testWidget0 do "set itk_component($name)"]
} [list $win $win]
}
test widget-1.7 {check the propagation of configuration options} {
list [.testWidget0 component hull cget -cursor] \
[.testWidget0 component test1 cget -cursor] \
[.testWidget0 component test2 cget -cursor]
} {trek trek trek}
test widget-1.8 {check the propagation of configuration options} {
list [.testWidget0 component hull cget -background] \
[.testWidget0 component test1 cget -background] \
[.testWidget0 component test2 cget -background]
} {red red MistyRose2}
test widget-1.9 {check the propagation of configuration options} {
list [.testWidget0 component test1 cget -text] \
[.testWidget0 component test2 cget -text]
} {Label: {Push Me}}
test widget-1.10 {check the invocation of "config" code} {
.testWidget0 do {set status}
} {{} {test message}}
test widget-1.11a {configure using the "code" command} {
.testWidget0 do {configure -command [itcl::code $this action "button press"]}
.testWidget0 cget -command
} {namespace inscope ::TestWidget {::.testWidget0 action {button press}}}
test widget-1.11b {execute some code created by "code" command} {
.testWidget0 do {set status ""}
.testWidget0 component test2 invoke
.testWidget0 configure -status "in between"
.testWidget0 component test2 invoke
.testWidget0 do {set status}
} {{button press} {in between} {button press}}
test widget-1.12a {components can be added on the fly} {
.testWidget0 do {
itk_component add test3 {
label $itk_interior.t3 -text "Temporary"
} {
keep -background -foreground -cursor
}
}
} {test3}
test widget-1.12b {components can be added on the fly} {
.testWidget0 do {
pack $itk_component(test3) -fill x
}
} {}
test widget-1.13 {new components show up on the component list} {
lsort [.testWidget0 component]
} {hull test1 test2 test3}
test widget-1.14 {new components are initialized properly} {
list [.testWidget0 component test3 cget -background] \
[.testWidget0 component test3 cget -foreground] \
[.testWidget0 component test3 cget -cursor]
} {red IndianRed trek}
test widget-1.15 {components can be deleted like ordinary widgets} {
destroy [.testWidget0 component test3]
} {}
test widget-1.16 {dead components are removed from the component list} {
lsort [.testWidget0 component]
} {hull test1 test2}
test widget-1.17 {use "configbody" command to change "config" code} {
itcl::configbody TestWidget::status {lappend status "new"}
} {}
test widget-1.18 {"config" code can really change} {
.testWidget0 do {set status ""}
.testWidget0 configure -status "test message"
.testWidget0 configure -status "another"
.testWidget0 do {set status}
} {new new}
test widget-1.19 {"config" code can change back} {
itcl::configbody TestWidget::status {lappend status $itk_option(-status)}
} {}
test widget-1.20 {mega-widgets show up on the object list} {
itcl::find objects .testWidget*
} {.testWidget0}
test widget-1.21 {when a mega-widget is destroyed, its object is deleted} {
destroy .testWidget0
itcl::find objects .testWidget*
} {}
test widget-1.22 {recreate a test widget} {
TestWidget .testWidget0
itcl::find objects .testWidget*
} {.testWidget0}
test widget-1.23 {when an object is deleted the widget is destroyed} {
itcl::delete object .testWidget0
winfo exists .testWidget0
} {0}
test widget-1.24 {recreate another test widget} {
TestWidget .testWidget
} {.testWidget}
test widget-1.25 {when an internal component is destroyed, it is removed from the list of components, and any dead options disappear} {
list [lsort [.testWidget component]] \
[.testWidget configure] \
[catch {destroy [.testWidget component test1]}] \
[.testWidget component] \
[.testWidget do {return [lsort [array names itk_component]]}] \
[.testWidget configure]
} {{hull test1 test2} {{-background background Background linen linen} {-borderwidth borderWidth BorderWidth 2 2} {-clientdata clientData ClientData {} {}} {-command command Command {} {}} {-cursor cursor Cursor {} {}} {-foreground foreground Foreground navy navy} {-highlight highlight Foreground white white} {-normal normal Background ivory ivory} {-status status Status {} {}} {-text text Text {} {}}} 0 {hull test2} {hull test2} {{-background background Background linen linen} {-borderwidth borderWidth BorderWidth 2 2} {-clientdata clientData ClientData {} {}} {-command command Command {} {}} {-cursor cursor Cursor {} {}} {-foreground foreground Foreground navy navy} {-highlight highlight Foreground white white} {-normal normal Background ivory ivory} {-status status Status {} {}}}}
test widget-1.26 {when an internal component is deleted (but not destroyed) it is disconnected from the option list and its binding tags are updated} {
set comp [.testWidget component test2]
list [bindtags $comp] \
[bind itk-destroy-$comp <Destroy>] \
[catch {.testWidget do {itk_component delete test2}}] \
[bindtags $comp] \
[bind itk-destroy-$comp <Destroy>] \
[.testWidget configure]
} {{itk-destroy-.testWidget.t2 .testWidget.t2 Button . all} {namespace inscope ::itk::Archetype {::.testWidget itk_component delete test2}} 0 {.testWidget.t2 Button . all} {} {{-background background Background linen linen} {-clientdata clientData ClientData {} {}} {-cursor cursor Cursor {} {}} {-status status Status {} {}}}}
test widget-1.27 {when a mega-widget object is deleted, its window and any
components are destroyed (even if in another window) } {
catch {destroy .t1}
catch {rename .t1.bw {}}
catch {itcl::delete class ButtonWidget}
itcl::class ButtonWidget {
inherit itk::Widget
constructor {args} {
eval itk_initialize $args
itk_component add button {
button $itk_option(-container).b -text Button
} {}
pack $itk_component(button)
}
itk_option define -container container Container {}
}
toplevel .t1
frame .t1.f
ButtonWidget .t1.bw -container .t1.f
pack .t1.f
pack .t1.bw
set button [.t1.bw component button]
itcl::delete object .t1.bw
set result [list $button [winfo exists $button]]
destroy .t1
itcl::delete class ButtonWidget
set result
} {.t1.f.b 0}
test widget-1.28 {when a window that contains a megawidget component
is destroyed, the component is removed from the megawidget} {
catch {destroy .t1}
catch {rename .t1.bw {}}
catch {itcl::delete class ButtonWidget}
itcl::class ButtonWidget {
inherit itk::Widget
constructor {args} {
eval itk_initialize $args
itk_component add button {
button $itk_option(-container).b -text Button
} {}
pack $itk_component(button)
}
itk_option define -container container Container {}
}
toplevel .t1
frame .t1.f
ButtonWidget .t1.bw -container .t1.f
pack .t1.f
pack .t1.bw
set result [list [.t1.bw component]]
destroy .t1.f
lappend result [list [.t1.bw component]]
itcl::delete object .t1.bw
destroy .t1
itcl::delete class ButtonWidget
set result
} {{button hull} hull}
test widget-1.29 {when destroying a component that is inside another
window protect against that case where one component destroy
actually destroys other contained components} {
catch {destroy .t1}
catch {rename .t1.bw {}}
catch {itcl::delete class ButtonWidget}
itcl::class ButtonWidget {
inherit itk::Widget
constructor {args} {
eval itk_initialize $args
# Note, the component names matter here since
# [.t2 component] returns names in hash order.
# We need to delete cframe first since it
# is the parent of cbutton.
itk_component add cframe {
button $itk_option(-container).cframe
} {}
pack $itk_component(cframe)
itk_component add cbutton {
button $itk_component(cframe).b -text Button
} {}
pack $itk_component(cbutton)
}
itk_option define -container container Container {}
}
toplevel .t1
frame .t1.f
ButtonWidget .t1.bw -container .t1.f
pack .t1.f
pack .t1.bw
set result [list [.t1.bw component]]
# destructor should destroy cframe but not cbutton
itcl::delete object .t1.bw
lappend result [winfo exists .t1.f.cframe]
destroy .t1
itcl::delete class ButtonWidget
set result
} {{hull cframe cbutton} 0}
# ----------------------------------------------------------------------
# Clean up
# ----------------------------------------------------------------------
itcl::delete class TestWidget
::tcltest::cleanupTests
exit
|