Debian Codegraph @

t/button-tcl.t

raw
1 #!/usr/bin/perl -w
2 # -*- perl -*-
3
4 # This file is a Tcl script to test labels, buttons, checkbuttons, and
5 # radiobuttons in Tk (i.e., all the widgets defined in tkButton.c). It is
6 # organized in the standard fashion for Tcl tests.
7 #
8 # Copyright (c) 1994 The Regents of the University of California.
9 # Copyright (c) 1994-1996 Sun Microsystems, Inc.
10 # Copyright (c) 1998-1999 by Scriptics Corporation.
11 # All rights reserved.
12 #
13 # RCS: @(#) $Id: button.test,v 1.20 2004/12/07 21:22:19 dgp Exp $
14
15 #
16 # Translated by Slaven Rezic (2007-07, from CVS version 1.20)
17 #
18
19 use strict;
20
21 use Tk;
22 use Getopt::Long;
23
24 BEGIN {
25 if (!eval q{
26 use Test::More;
27 1;
28 }) {
29 print "1..0 # skip: no Test::More module\n";
30 exit;
31 }
32 }
33
34 plan tests => 288;
35
36 my $mw = MainWindow->new;
37 $mw->geometry("+10+10");
38
39 # proc bogusTrace args {
40 # error "trace aborted"
41 # }
42 my $value;
43 my $value2;
44
45 # Create entries in the option database to be sure that geometry options
46 # like border width have predictable values.
47
48 $mw->option('add', '*Button.borderWidth', 2);
49 $mw->option('add', '*Button.highlightThickness', 2);
50 $mw->option('add', '*Button.font', '{Helvetica -12 bold}');
51
52 $mw->Photo('image1', -file => Tk->findINC("Xcamel.gif"));
53
54 my $l = $mw->Label(Name => "l", qw(-text Label))->pack;
55 my $b = $mw->Button(Name => "b", qw(-text Button))->pack;
56 my $c = $mw->Checkbutton(Name => "c", qw(-text Checkbutton))->pack;
57 my $r = $mw->Radiobutton(Name => "r", qw(-text Radiobutton))->pack;
58 $mw->update;
59
60 use constant SKIP_CGET => 5;
61 use constant SKIP_CONF => 6;
62 use constant SKIP_ERROR => 7;
63 use constant SKIP_RESTORE => 8;
64
65 sub test_command { }
66 my $test_command = $mw->Callback(\&test_command);
67 my $test_font = $mw->fontCreate(-family => 'helvetica', -size => 10);
68 my $test_variable = "foo";
69
70 my @tests =
71 (
72 ['-activebackground', '#012345', '#012345', 'non-existent',
73 q{unknown color name "non-existent"}, qw{1 1 1 1}
74 ],
75 ['-activeforeground', '#ff0000', '#ff0000', 'non-existent',
76 q{unknown color name "non-existent"}, qw{1 1 1 1}
77 ],
78 ['-anchor', 'nw', 'nw', 'bogus',
79 q{bad anchor "bogus": must be n, ne, e, se, s, sw, w, nw, or center},
80 qw{1 1 1 1}
81 ],
82 ['-background', '#ff0000', '#ff0000', 'non-existent',
83 q{unknown color name "non-existent"}, qw{1 1 1 1}
84 ],
85 ['-bd', '4', '4', 'badValue', q{bad screen distance "badValue"}, qw{1 1 1 1}],
86 ['-bg', '#ff0000', '#ff0000', 'non-existent', q{unknown color name "non-existent"},
87 qw{1 1 1 1}
88 ],
89 ['-bitmap', 'questhead', 'questhead', 'badValue', q{bitmap "badValue" not defined},
90 qw{1 1 1 1}
91 ],
92 ['-borderwidth', '1.3', '1.3', 'badValue', q{bad screen distance "badValue"}, qw{1 1 1 1}],
93 ['-command', $test_command, $test_command, q{}, q{}, qw{0 1 1 1}],
94 ['-compound', 'left', 'left', 'bogus',
95 q{bad compound "bogus": must be bottom, center, left, none, right, or top},
96 qw{1 1 1 1}],
97 ['-cursor', 'arrow', 'arrow', 'badValue', q{bad cursor spec "badValue"}, qw{1 1 1 1}],
98 ['-default', 'active', 'active', 'huh?',
99 q{bad default "huh?": must be active, disabled, or normal},
100 qw{0 1 0 0}],
101 ['-disabledforeground', '#00ff00', '#00ff00', 'xyzzy', q{unknown color name "xyzzy"},
102 qw{1 1 1 1}],
103 ['-fg', '#110022', '#110022', 'bogus', q{unknown color name "bogus"}, qw{1 1 1 1}],
104 ['-font', $test_font, $test_font, q{}, q{font "" doesn't exist}, qw{1 1 1 1}],
105 ['-foreground', '#110022', '#110022', 'bogus', q{unknown color name "bogus"}, qw{1 1 1 1}],
106 ['-height', '18', '18', '20x', q{bad screen distance "20x"}, qw{1 1 1 1}], # original error 'was expected integer but got "20.0"', but Perl does not care about float vs. integer
107 ['-highlightbackground', '#112233', '#112233', 'ugly', q{unknown color name "ugly"},
108 qw{1 1 1 1}],
109 ['-highlightcolor', '#110022', '#110022', 'bogus', q{unknown color name "bogus"},
110 qw{1 1 1 1}],
111 ['-highlightthickness', '6m', '6m', 'badValue', q{bad screen distance "badValue"},
112 qw{1 1 1 1}],
113 ['-image', 'image1', 'image1', 'bogus', q{image "bogus" doesn't exist}, qw{1 1 1 1}],
114 ['-indicatoron', 'yes', '1', '', '', # XXX no error in Perl/Tk: 'no_way', q{expected boolean value but got "no_way"},
115 qw{0 0 1 1}],
116 ['-justify', 'right', 'right', 'bogus',
117 q{bad justification "bogus": must be left, right, or center},
118 qw{1 1 1 1}],
119 ['-offrelief', 'flat', 'flat', '1.5',
120 q{bad relief "1.5": must be flat, groove, raised, ridge, solid, or sunken},
121 qw{0 0 1 1}],
122 ['-offvalue', 'lousy', 'lousy', q{}, q{}, qw{0 0 1 0}],
123 ['-onvalue', 'fantastic', 'fantastic', q{}, q{}, qw{0 0 1 0}],
124 ['-overrelief', '', '', '1.5',
125 q{bad relief "1.5": must be flat, groove, raised, ridge, solid, or sunken},
126 qw(0 1 1 1)],
127 ['-padx', '12m', '12m', '420x', q{bad screen distance "420x"}, qw{1 1 1 1}],
128 ['-pady', '12m', '12m', '420x', q{bad screen distance "420x"}, qw{1 1 1 1}],
129 ['-repeatdelay', '100', '100', 'foo', q{'foo' isn't numeric}, qw{0 1 0 0}], # original error 'expected integer but got "foo"'
130 ['-repeatinterval', '100', '100', 'foo', q{'foo' isn't numeric}, qw{0 1 0 0}], # original error 'expected integer but got "foo"'
131 ['-relief', 'flat', 'flat', '1.5',
132 q{bad relief "1.5": must be flat, groove, raised, ridge, solid, or sunken},
133 qw{1 1 1 1}],
134 ['-selectcolor', '#110022', '#110022', 'bogus', q{unknown color name "bogus"}, qw{0 0 1 1}],
135 ['-selectimage', 'image1', 'image1', 'bogus', q{image "bogus" doesn't exist}, qw{0 0 1 1}],
136 ['-state', 'normal', 'normal', 'bogus',
137 q{bad state "bogus": must be active, disabled, or normal},
138 qw{1 1 1 1}],
139 ['-takefocus', 'any string', 'any string', q{}, q{}, qw{1 1 1 1}],
140 ['-text', 'Sample text', q{Sample text}, q{}, q{}, qw{1 1 1 1}],
141 ['-textvariable', \$test_variable, \$test_variable, q{}, q{}, qw{1 1 1 1}],
142 ## not in 8.4
143 # ['-tristateimage', 'image1', 'image1', 'bogus', q{image "bogus" doesn't exist},
144 # qw{0 0 1 1}],
145 # ['-tristatevalue', 'unknowable', 'unknowable', q{}, q{}, qw{0 0 1 1}],
146 ['-underline', '5', '5', '3p', q{'3p' isn't numeric}, qw{1 1 1 1}], # original error 'expected integer but got "3p"'
147 ['-value', 'anyString', 'anyString', q{}, q{}, qw{0 0 0 1}],
148 ['-width', '402', '402', '', '', # no error in Perl/Tk: '3p', q{'3p' isn't numeric},
149 qw{1 1 1 1}],
150 ['-wraplength', '100', '100', '6x', q{bad screen distance "6x"}, qw{1 1 1 1}],
151 );
152
153 foreach my $test (@tests) {
154 my($name, $value, $okResult, $badValue, $badResult, @classes) = @$test;
155 for my $i (0 .. 3) {
156 my $w_name = [qw(l b c r)]->[$i];
157 my $hasOption = $classes[$i];
158 my $w = $mw->Widget(".$w_name");
159 die "Cannot get widget from .$w_name" if !$w;
160 my $classname = $w->class;
161 if ($hasOption) {
162 $w->configure($name, $value);
163 is(($w->configure($name))[4], $okResult, "configuration option $name for $classname");
164
165 if ($badValue ne "") {
166 eval {
167 $w->configure($name, $badValue);
168 };
169 like($@, qr{\Q$badResult}, "bad value for configuration option $name for $classname");
170 }
171 } else {
172 eval {
173 $w->configure($name, $value);
174 };
175 like($@, qr{^\Qunknown option "$name"}, "unknown configuration option $name for $classname");
176 }
177
178 }
179 }
180
181 # Additional check to make sure that -selectcolor may be empty in
182 # checkbox widgets
183 $c->configure(-selectcolor => '');
184 pass('Empty selectcolor in Checkbutton');
185
186 for my $class (qw(Label Button Checkbutton Radiobutton)) {
187 my $x = $mw->$class;
188 is($x->class, $class, "ButtonCreate procedure - setting label $class");
189 }
190
191 {
192 $mw->optionAdd('*funny.background', 'bogus');
193 eval {
194 $mw->Button(Name => "funny");
195 };
196 like($@, qr{\Qunknown color name "bogus" at},
197 "ButtonCreate procedure - error in default option value");
198 }
199
200 {
201 my $w = eval {
202 $mw->Button(qw(-gorp foo));
203 };
204 like($@, qr{\Qunknown option "-gorp"}, "ButtonCreate procedure - option error");
205 ok(!Tk::Exists($w), "Button did not get created");
206 }
207
208 __END__
209
210 test button-4.1 {ButtonWidgetCmd - too few arguments} {
211 list [catch {.b} msg] $msg
212 } {1 {wrong # args: should be ".b option ?arg arg ...?"}}
213 test button-4.2 {ButtonWidgetCmd - bad option name} {
214 list [catch {.b c} msg] $msg
215 } {1 {ambiguous option "c": must be cget, configure, flash, or invoke}}
216 test button-4.3 {ButtonWidgetCmd - bad option name} {
217 list [catch {.b bogus} msg] $msg
218 } {1 {bad option "bogus": must be cget, configure, flash, or invoke}}
219 test button-4.4 {ButtonWidgetCmd procedure, "cget" option} {
220 list [catch {.b cget a b} msg] $msg
221 } {1 {wrong # args: should be ".b cget option"}}
222 test button-4.5 {ButtonWidgetCmd procedure, "cget" option} {
223 list [catch {.b cget -gorp} msg] $msg
224 } {1 {unknown option "-gorp"}}
225 test button-4.6 {ButtonWidgetCmd procedure, "cget" option} {
226 .b configure -highlightthickness 3
227 .b cget -highlightthickness
228 } {3}
229 test button-4.7 {ButtonWidgetCmd procedure, "cget" option} {
230 catch {.l cget -disabledforeground}
231 } {0}
232 test button-4.8 {ButtonWidgetCmd procedure, "cget" option} {
233 catch {.b cget -disabledforeground}
234 } {0}
235 test button-4.9 {ButtonWidgetCmd procedure, "cget" option} {
236 list [catch {.b cget -variable} msg] $msg
237 } {1 {unknown option "-variable"}}
238 test button-4.10 {ButtonWidgetCmd procedure, "cget" option} {
239 catch {.c cget -variable}
240 } {0}
241 test button-4.11 {ButtonWidgetCmd procedure, "cget" option} {
242 list [catch {.c cget -value} msg] $msg
243 } {1 {unknown option "-value"}}
244 test button-4.12 {ButtonWidgetCmd procedure, "cget" option} {
245 catch {.r cget -value}
246 } {0}
247 test button-4.13 {ButtonWidgetCmd procedure, "cget" option} {
248 list [catch {.r cget -onvalue} msg] $msg
249 } {1 {unknown option "-onvalue"}}
250 test button-4.14 {ButtonWidgetCmd procedure, "configure" option} {
251 llength [.c configure]
252 } {41}
253 test button-4.15 {ButtonWidgetCmd procedure, "configure" option} {
254 list [catch {.b configure -gorp} msg] $msg
255 } {1 {unknown option "-gorp"}}
256 test button-4.16 {ButtonWidgetCmd procedure, "configure" option} {
257 list [catch {.b co -bg #ffffff -fg} msg] $msg
258 } {1 {value for "-fg" missing}}
259 test button-4.17 {ButtonWidgetCmd procedure, "configure" option} {
260 .b configure -fg #123456
261 .b configure -bg #654321
262 lindex [.b configure -fg] 4
263 } {#123456}
264 .c configure -variable value -onvalue 1 -offvalue 0
265 .r configure -variable value2 -value red
266 test button-4.18 {ButtonWidgetCmd procedure, "deselect" option} {
267 list [catch {.c deselect foo} msg] $msg
268 } {1 {wrong # args: should be ".c deselect"}}
269 test button-4.19 {ButtonWidgetCmd procedure, "deselect" option} {
270 list [catch {.l deselect} msg] $msg
271 } {1 {bad option "deselect": must be cget or configure}}
272 test button-4.20 {ButtonWidgetCmd procedure, "deselect" option} {
273 list [catch {.b deselect} msg] $msg
274 } {1 {bad option "deselect": must be cget, configure, flash, or invoke}}
275 test button-4.21 {ButtonWidgetCmd procedure, "deselect" option} {
276 set value 1
277 .c d
278 set value
279 } {0}
280 test button-4.22 {ButtonWidgetCmd procedure, "deselect" option} {
281 set value2 green
282 .r deselect
283 set value2
284 } {green}
285 test button-4.23 {ButtonWidgetCmd procedure, "deselect" option} {
286 set value2 red
287 .r deselect
288 set value2
289 } {}
290 test button-4.24 {ButtonWidgetCmd procedure, "deselect" option} -body {
291 set value 1
292 trace variable value w bogusTrace
293 set result [list [catch {.c deselect} msg] $msg $errorInfo $value]
294 trace vdelete value w bogusTrace
295 set result
296 } -match glob -result {1 {can't set "value": trace aborted} {*trace aborted
297 while executing
298 *
299 ".c deselect"} 0}
300 test button-4.25 {ButtonWidgetCmd procedure, "deselect" option} -body {
301 set value2 red
302 trace variable value2 w bogusTrace
303 set result [list [catch {.r deselect} msg] $msg $errorInfo $value2]
304 trace vdelete value2 w bogusTrace
305 set result
306 } -match glob -result {1 {can't set "value2": trace aborted} {*trace aborted
307 while executing
308 *
309 ".r deselect"} {}}
310 test button-4.26 {ButtonWidgetCmd procedure, "flash" option} {
311 list [catch {.b flash foo} msg] $msg
312 } {1 {wrong # args: should be ".b flash"}}
313 test button-4.27 {ButtonWidgetCmd procedure, "flash" option} {
314 list [catch {.l flash} msg] $msg
315 } {1 {bad option "flash": must be cget or configure}}
316 test button-4.28 {ButtonWidgetCmd procedure, "flash" option} {
317 list [catch {.b flash} msg] $msg
318 } {0 {}}
319 test button-4.29 {ButtonWidgetCmd procedure, "flash" option} {
320 list [catch {.c flash} msg] $msg
321 } {0 {}}
322 test button-4.30 {ButtonWidgetCmd procedure, "flash" option} {
323 list [catch {.r f} msg] $msg
324 } {0 {}}
325 test button-4.31 {ButtonWidgetCmd procedure, "invoke" option} {
326 list [catch {.b invoke foo} msg] $msg
327 } {1 {wrong # args: should be ".b invoke"}}
328 test button-4.32 {ButtonWidgetCmd procedure, "invoke" option} {
329 list [catch {.l invoke} msg] $msg
330 } {1 {bad option "invoke": must be cget or configure}}
331 test button-4.33 {ButtonWidgetCmd procedure, "invoke" option} {
332 .b configure -command {set x invoked}
333 set x "not invoked"
334 .b invoke
335 set x
336 } {invoked}
337 test button-4.34 {ButtonWidgetCmd procedure, "invoke" option} {
338 .b configure -command {set x invoked} -state disabled
339 set x "not invoked"
340 .b invoke
341 set x
342 } {not invoked}
343 test button-4.35 {ButtonWidgetCmd procedure, "invoke" option} {
344 set value bogus
345 .c configure -command {set x invoked} -variable value -onvalue 1 \
346 -offvalue 0
347 set x "not invoked"
348 .c invoke
349 list $x $value
350 } {invoked 1}
351 test button-4.36 {ButtonWidgetCmd procedure, "invoke" option} {
352 set value2 green
353 .r configure -command {set x invoked} -variable value2 -value red
354 set x "not invoked"
355 .r i
356 list $x $value2
357 } {invoked red}
358 test button-4.37 {ButtonWidgetCmd procedure, "select" option} {
359 list [catch {.l select} msg] $msg
360 } {1 {bad option "select": must be cget or configure}}
361 test button-4.38 {ButtonWidgetCmd procedure, "select" option} {
362 list [catch {.b select} msg] $msg
363 } {1 {bad option "select": must be cget, configure, flash, or invoke}}
364 test button-4.39 {ButtonWidgetCmd procedure, "select" option} {
365 list [catch {.c select foo} msg] $msg
366 } {1 {wrong # args: should be ".c select"}}
367 test button-4.40 {ButtonWidgetCmd procedure, "select" option} {
368 set value bogus
369 .c configure -command {} -variable value -onvalue lovely -offvalue 0
370 .c s
371 set value
372 } {lovely}
373 test button-4.41 {ButtonWidgetCmd procedure, "select" option} {
374 set value2 green
375 .r configure -command {} -variable value2 -value red
376 .r select
377 set value2
378 } {red}
379 test button-4.42 {ButtonWidgetCmd procedure, "select" option} -body {
380 set value2 yellow
381 trace variable value2 w bogusTrace
382 set result [list [catch {.r select} msg] $msg $errorInfo $value2]
383 trace vdelete value2 w bogusTrace
384 set result
385 } -match glob -result {1 {can't set "value2": trace aborted} {*trace aborted
386 while executing
387 *
388 ".r select"} red}
389 test button-4.43 {ButtonWidgetCmd procedure, "toggle" option} {
390 list [catch {.l toggle} msg] $msg
391 } {1 {bad option "toggle": must be cget or configure}}
392 test button-4.44 {ButtonWidgetCmd procedure, "toggle" option} {
393 list [catch {.b toggle} msg] $msg
394 } {1 {bad option "toggle": must be cget, configure, flash, or invoke}}
395 test button-4.45 {ButtonWidgetCmd procedure, "toggle" option} {
396 list [catch {.r toggle} msg] $msg
397 } {1 {bad option "toggle": must be cget, configure, deselect, flash, invoke, or select}}
398 test button-4.46 {ButtonWidgetCmd procedure, "toggle" option} {
399 list [catch {.c toggle foo} msg] $msg
400 } {1 {wrong # args: should be ".c toggle"}}
401 test button-4.47 {ButtonWidgetCmd procedure, "toggle" option} {
402 set value bogus
403 .c configure -command {} -variable value -onvalue sunshine -offvalue rain
404 .c toggle
405 set result $value
406 .c toggle
407 lappend result $value
408 .c toggle
409 lappend result $value
410 } {sunshine rain sunshine}
411 test button-4.48 {ButtonWidgetCmd procedure, "toggle" option} -body {
412 .c configure -onvalue xyz -offvalue abc
413 set value xyz
414 trace variable value w bogusTrace
415 set result [list [catch {.c toggle} msg] $msg $errorInfo $value]
416 trace vdelete value w bogusTrace
417 set result
418 } -match glob -result {1 {can't set "value": trace aborted} {*trace aborted
419 while executing
420 *
421 ".c toggle"} abc}
422 test button-4.49 {ButtonWidgetCmd procedure, "toggle" option} -body {
423 .c configure -onvalue xyz -offvalue abc
424 set value abc
425 trace variable value w bogusTrace
426 set result [list [catch {.c toggle} msg] $msg $errorInfo $value]
427 trace vdelete value w bogusTrace
428 set result
429 } -match glob -result {1 {can't set "value": trace aborted} {*trace aborted
430 while executing
431 *
432 ".c toggle"} xyz}
433 test button-4.50 {ButtonWidgetCmd procedure, "toggle" option} {
434 catch {unset value}; set value(1) 1;
435 set result [list [catch {.c toggle} msg] $msg $errorInfo]
436 unset value;
437 set result
438 } {1 {can't set "value": variable is array} {can't set "value": variable is array
439 while executing
440 ".c toggle"}}
441
442 test button-5.1 {DestroyButton procedure} testImageType {
443 image create test image1
444 button .b1 -image image1
445 button .b2 -fg #ff0000 -text "Button 2"
446 button .b3 -state active -text "Button 3"
447 button .b4 -disabledforeground #0000ff -state disabled -text "Button 4"
448 checkbutton .b5 -variable x -text "Checkbutton 5"
449 set x 1
450 pack .b1 .b2 .b3 .b4 .b5
451 update
452 deleteWindows
453 } {}
454
455 test button-6.1 {ConfigureButton - textvariable trace} {
456 catch {destroy .b1}
457 button .b1 -bd 4 -bg green
458 catch {.b1 configure -bd 7 -bg green -fg bogus}
459 list [catch {.b1 configure -bd 7 -bg red -fg bogus} msg] \
460 $msg [.b1 cget -bd] [.b1 cget -bg]
461 } {1 {unknown color name "bogus"} 4 green}
462 test button-6.2 {ConfigureButton - textvariable trace} {
463 catch {destroy .b1}
464 set x From-x
465 set y From-y
466 button .b1 -textvariable x
467 .b1 configure -textvariable y
468 set x New
469 lindex [.b1 configure -text] 4
470 } {From-y}
471 test button-6.2a {ConfigureButton - variable traces} {
472 catch {destroy .b1}
473 catch {unset x}
474 checkbutton .b1 -variable x
475 set x 1
476 set y 1
477 .b1 configure -textvariable y
478 set x 0
479 .b1 toggle
480 set y
481 } {1}
482 test button-6.3 {ConfigureButton - image handling} testImageType {
483 catch {destroy .b1}
484 eval image delete [image names]
485 image create test image1
486 image create test image2
487 button .b1 -image image1
488 image delete image1
489 .b1 configure -image image2
490 image names
491 } {image2}
492 test button-6.5 {ConfigureButton - default value for variable} {
493 catch {destroy .b1}
494 checkbutton .b1
495 .b1 cget -variable
496 } {b1}
497 test button-6.6 {ConfigureButton - setting selected state from variable} {
498 catch {destroy .b1}
499 set x 0
500 set y Shiny
501 checkbutton .b1 -variable x
502 .b1 configure -variable y -onvalue Shiny
503 .b1 toggle
504 set y
505 } 0
506 test button-6.7 {ConfigureButton - setting selected state from variable} {
507 catch {destroy .b1}
508 catch {unset x}
509 checkbutton .b1 -variable x -offvalue Bogus
510 set x
511 } Bogus
512 test button-6.8 {ConfigureButton - setting selected state from variable} {
513 catch {destroy .b1}
514 catch {unset x}
515 radiobutton .b1 -variable x
516 set x
517 } {}
518 test button-6.9 {ConfigureButton - error in setting variable} {
519 catch {destroy .b1}
520 catch {unset x}
521 trace variable x w bogusTrace
522 set result [list [catch {radiobutton .b1 -variable x} msg] $msg]
523 trace vdelete x w bogusTrace
524 set result
525 } {1 {can't set "x": trace aborted}}
526 test button-6.10 {ConfigureButton - bad image name} {
527 catch {destroy .b1}
528 list [catch {button .b1 -image bogus} msg] $msg
529 } {1 {image "bogus" doesn't exist}}
530 test button-6.11 {ConfigureButton - setting variable from current text value} {
531 catch {destroy .b1}
532 catch {unset x}
533 button .b1 -textvariable x -text "Button 1"
534 set x
535 } {Button 1}
536 test button-6.12 {ConfigureButton - using current value of variable} {
537 catch {destroy .b1}
538 set x Override
539 button .b1 -textvariable x -text "Button 1"
540 set x
541 } {Override}
542 test button-6.13 {ConfigureButton - variable handling} {
543 catch {destroy .b1}
544 catch {unset x}
545 trace variable x w bogusTrace
546 set result [list [catch {radiobutton .b1 -text foo -textvariable x} msg] \
547 $msg $x]
548 trace vdelete x w bogusTrace
549 set result
550 } {1 {can't set "x": trace aborted} foo}
551 test button-6.14 {ConfigureButton - -width option} {
552 catch {destroy .b1}
553 button .b1 -text "Button 1"
554 list [catch {.b1 configure -width 1i} msg] $msg $errorInfo
555 } {1 {expected integer but got "1i"} {expected integer but got "1i"
556 (processing -width option)
557 invoked from within
558 ".b1 configure -width 1i"}}
559 test button-6.15 {ConfigureButton - -height option} {
560 catch {destroy .b1}
561 button .b1 -text "Button 1"
562 list [catch {.b1 configure -height 0.5c} msg] $msg $errorInfo
563 } {1 {expected integer but got "0.5c"} {expected integer but got "0.5c"
564 (processing -height option)
565 invoked from within
566 ".b1 configure -height 0.5c"}}
567 test button-6.16 {ConfigureButton - -width option} {
568 catch {destroy .b1}
569 button .b1 -bitmap questhead
570 list [catch {.b1 configure -width abc} msg] $msg $errorInfo
571 } {1 {bad screen distance "abc"} {bad screen distance "abc"
572 (processing -width option)
573 invoked from within
574 ".b1 configure -width abc"}}
575 test button-6.17 {ConfigureButton - -height option} testImageType {
576 catch {destroy .b1}
577 eval image delete [image names]
578 image create test image1
579 button .b1 -image image1
580 list [catch {.b1 configure -height 0.5x} msg] $msg $errorInfo
581 } {1 {bad screen distance "0.5x"} {bad screen distance "0.5x"
582 (processing -height option)
583 invoked from within
584 ".b1 configure -height 0.5x"}}
585 test button-6.18 {ConfigureButton - computing geometry} {nonPortable fonts} {
586 catch {destroy .b1}
587 button .b1 -text "Sample text" -width 10 -height 2
588 pack .b1
589 set result "[winfo reqwidth .b1] [winfo reqheight .b1]"
590 .b1 configure -bitmap questhead
591 lappend result [winfo reqwidth .b1] [winfo reqheight .b1]
592 } {102 46 20 12}
593 test button-6.19 {ConfigureButton - computing geometry} {
594 catch {destroy .b1}
595 button .b1 -text "Button 1"
596 set old [winfo reqwidth .b1]
597 .b1 configure -text "Much longer text"
598 set new [winfo reqwidth .b1]
599 expr $old == $new
600 } {0}
601
602 test button-7.1 {ButtonEventProc procedure} {
603 catch {destroy .b1}
604 button .b1 -text "Test Button" -command {
605 destroy .b1
606 set x [list [winfo exists .b1] [info commands .b1]]
607 }
608 .b1 invoke
609 set x
610 } {0 {}}
611 test button-7.2 {ButtonEventProc procedure} {
612 deleteWindows
613 button .b1 -bg #543210
614 rename .b1 .b2
615 set x {}
616 lappend x [winfo children .]
617 lappend x [.b2 cget -bg]
618 destroy .b1
619 lappend x [info command .b*] [winfo children .]
620 } {.b1 #543210 {} {}}
621
622 test button-8.1 {ButtonCmdDeletedProc procedure} {
623 deleteWindows
624 button .b1
625 rename .b1 {}
626 list [info command .b*] [winfo children .]
627 } {{} {}}
628
629 test button-9.1 {TkInvokeButton procedure} {
630 catch {destroy .b1}
631 set x 0
632 checkbutton .b1 -variable x
633 set result $x
634 .b1 invoke
635 lappend result $x
636 .b1 invoke
637 lappend result $x
638 } {0 1 0}
639 test button-9.2 {TkInvokeButton procedure} {
640 catch {destroy .b1}
641 set x 0
642 checkbutton .b1 -variable x
643 trace variable x w bogusTrace
644 set result [list [catch {.b1 invoke} msg] $msg $x]
645 trace vdelete x w bogusTrace
646 set result
647 } {1 {can't set "x": trace aborted} 1}
648 test button-9.3 {TkInvokeButton procedure} {
649 catch {destroy .b1}
650 set x 1
651 checkbutton .b1 -variable x
652 trace variable x w bogusTrace
653 set result [list [catch {.b1 invoke} msg] $msg $x]
654 trace vdelete x w bogusTrace
655 set result
656 } {1 {can't set "x": trace aborted} 0}
657 test button-9.4 {TkInvokeButton procedure} {
658 catch {destroy .b1}
659 set x 0
660 radiobutton .b1 -variable x -value red
661 set result $x
662 .b1 invoke
663 lappend result $x
664 .b1 invoke
665 lappend result $x
666 } {0 red red}
667 test button-9.5 {TkInvokeButton procedure} -body {
668 catch {destroy .b1}
669 radiobutton .b1 -variable x -value red
670 set x green
671 trace variable x w bogusTrace
672 set result [list [catch {.b1 invoke} msg] $msg $errorInfo $x]
673 trace vdelete x w bogusTrace
674 set result
675 } -match glob -result {1 {can't set "x": trace aborted} {*trace aborted
676 while executing
677 *
678 ".b1 invoke"} red}
679 test button-9.6 {TkInvokeButton procedure} {
680 deleteWindows
681 set result untouched
682 button .b1 -command {set result invoked}
683 list [catch {.b1 invoke} msg] $msg $result
684 } {0 invoked invoked}
685 test button-9.7 {TkInvokeButton procedure} {
686 deleteWindows
687 set result untouched
688 set x 0
689 checkbutton .b1 -variable x -command {set result "invoked $x"}
690 list [catch {.b1 invoke} msg] $msg $result
691 } {0 {invoked 1} {invoked 1}}
692 test button-9.8 {TkInvokeButton procedure} {
693 deleteWindows
694 set result untouched
695 set x 0
696 radiobutton .b1 -variable x -value red -command {set result "invoked $x"}
697 list [catch {.b1 invoke} msg] $msg $result
698 } {0 {invoked red} {invoked red}}
699
700 test button-10.1 {ButtonVarProc procedure} {
701 deleteWindows
702 set x 1
703 checkbutton .b1 -variable x
704 unset x
705 set result [info exists x]
706 .b1 toggle
707 lappend result $x
708 set x 0
709 .b1 toggle
710 lappend result $x
711 } {0 1 1}
712 test button-10.2 {ButtonVarProc procedure} {
713 deleteWindows
714 set x 0
715 checkbutton .b1 -variable x
716 set x 44
717 .b1 toggle
718 set x
719 } {1}
720 test button-10.3 {ButtonVarProc procedure} {
721 deleteWindows
722 set x 1
723 checkbutton .b1 -variable x
724 set x 44
725 .b1 toggle
726 set x
727 } {1}
728 test button-10.4 {ButtonVarProc procedure} {
729 deleteWindows
730 set x 0
731 checkbutton .b1 -variable x
732 set x 1
733 .b1 toggle
734 set x
735 } {0}
736 test button-10.5 {ButtonVarProc procedure} {
737 deleteWindows
738 set x 1
739 checkbutton .b1 -variable x
740 set x 1
741 .b1 toggle
742 set x
743 } {0}
744 test button-10.6 {ButtonVarProc procedure} {
745 deleteWindows
746 set x 0
747 checkbutton .b1 -variable x
748 set x 0
749 .b1 toggle
750 set x
751 } {1}
752 test button-10.7 {ButtonVarProc procedure} {
753 deleteWindows
754 set x 1
755 checkbutton .b1 -variable x
756 set x 0
757 .b1 toggle
758 set x
759 } {1}
760 test button-10.8 {ButtonVarProc procedure, can't read variable} {
761 # This test does nothing but produce a core dump if there's a problem.
762 deleteWindows
763 catch {unset a}
764 checkbutton .b1 -variable a
765 unset a
766 set a(32) 0
767 unset a
768 } {}
769
770 test button-11.1 {ButtonTextVarProc procedure} {
771 deleteWindows
772 set x Label
773 button .b1 -textvariable x
774 unset x
775 set result [list $x [lindex [.b1 configure -text] 4]]
776 set x New
777 lappend result [lindex [.b1 configure -text] 4]
778 } {Label Label New}
779 test button-11.2 {ButtonTextVarProc procedure} {
780 deleteWindows
781 # Windows buttons have a default min width, so we have to
782 # set this to be longer to force the wider button.
783 set x ExtraLongLabel
784 button .b1 -textvariable x
785 set old [winfo reqwidth .b1]
786 set x New
787 set new [winfo reqwidth .b1]
788 list [lindex [.b1 configure -text] 4] [expr $old == $new]
789 } {New 0}
790
791 test button-12.1 {ButtonImageProc procedure} testImageType {
792 deleteWindows
793 eval image delete [image names]
794 image create test image1
795 label .b1 -image image1 -padx 0 -pady 0 -bd 0
796 pack .b1
797 set result "[winfo reqwidth .b1] [winfo reqheight .b1]"
798 image1 changed 0 0 0 0 80 100
799 lappend result [winfo reqwidth .b1] [winfo reqheight .b1]
800 } {30 15 80 100}
801
802 deleteWindows
803 set l [interp hidden]
804
805 test button-13.1 {button widget vs hidden commands} {
806 catch {destroy .b}
807 button .b -text hello
808 interp hide {} .b
809 destroy .b
810 list [winfo children .] [interp hidden]
811 } [list {} $l]
812
813 deleteWindows
814
815 test button-14.1 {size behaviouor} {
816 set res {}
817 foreach class {label button radiobutton checkbutton} {
818 eval destroy [winfo children .]
819
820 $class .a -text Hej
821 $class .b -text Hej -width 10 -height 1
822 $class .c -text "" -width 10 -height 1
823
824 for {set t 0} {$t < 2} {incr t} {
825 set res2 {}
826 # With -width, width should not be affected by text change
827 lappend res2 [expr {[winfo reqwidth .b] == [winfo reqwidth .c]}]
828 # With -height, height should not be affected by text change
829 lappend res2 [expr {[winfo reqheight .b] == [winfo reqheight .c]}]
830 # A one line text should be as high as -height 1
831 lappend res2 [expr {[winfo reqheight .a] == [winfo reqheight .b]}]
832 lappend res $res2
833
834 # Do the second round with another font
835 .a configure -font "Arial 20"
836 .b configure -font "Arial 20"
837 .c configure -font "Arial 20"
838 }
839 }
840 set res
841 } {{1 1 1} {1 1 1} {1 1 1} {1 1 1} {1 1 1} {1 1 1} {1 1 1} {1 1 1}}
842
843 deleteWindows
844
845 option clear
846
847 # cleanup
848 cleanupTests
849 return
850
851 __END__