OSDN Git Service

Please enter the commit message for your changes. Lines starting
[eos/base.git] / util / src / TclTk / tcl8.6.12 / pkgs / itcl4.2.2 / tests / widgetclass.test
1 #---------------------------------------------------------------------
2 # TITLE:
3 #       widgetclass.test
4 #
5 # AUTHOR:
6 #       Arnulf Wiedemann with a lot of code form the snit tests by
7 #       Will Duquette
8 #
9 # DESCRIPTION:
10 #       Test cases for ::itcl::type command.
11 #       Uses the ::tcltest:: harness.
12 #
13 #       There is at least Tcl 8.6a3 needed
14 #
15 #    The tests assume tcltest 2.2
16 #-----------------------------------------------------------------------
17
18 # ### ### ### ######### ######### #########
19 ## Declare the minimal version of Tcl required to run the package
20 ## tested by this testsuite, and its dependencies.
21
22 proc testsNeedTcl {version} {
23     # This command ensures that a minimum version of Tcl is used to
24     # run the tests in the calling testsuite. If the minimum is not
25     # met by the active interpreter we forcibly bail out of the
26     # testsuite calling the command. The command has to be called
27     # immediately after loading the utilities.
28
29     if {[package vsatisfies [package provide Tcl] ${version}-]} return
30
31     puts "    Aborting the tests found in \"[file tail [info script]]\""
32     puts "    Requiring at least Tcl $version, have [package provide Tcl]."
33
34     # This causes a 'return' in the calling scope.
35     return -code return
36 }
37
38 # ### ### ### ######### ######### #########
39 ## Declare the minimum version of Tcltest required to run the
40 ## testsuite.
41
42 proc testsNeedTcltest {version} {
43     # This command ensure that a minimum version of the Tcltest
44     # support package is used to run the tests in the calling
45     # testsuite. If the minimum is not met by the loaded package we
46     # forcibly bail out of the testsuite calling the command. The
47     # command has to be called after loading the utilities. The only
48     # command allowed to come before it is 'textNeedTcl' above.
49
50     # Note that this command will try to load a suitable version of
51     # Tcltest if the package has not been loaded yet.
52
53     if {[lsearch [namespace children] ::tcltest] == -1} {
54         if {![catch {
55             package require tcltest $version
56         }]} {
57             namespace import -force ::tcltest::*
58             return
59         }
60     } elseif {[package vcompare [package present tcltest] $version] >= 0} {
61         namespace import -force ::tcltest::*
62         return
63     }
64
65     puts "    Aborting the tests found in [file tail [info script]]."
66     puts "    Requiring at least tcltest $version, have [package present tcltest]"
67
68     # This causes a 'return' in the calling scope.
69     return -code return
70 }
71
72 # Set up for Tk tests: enter the event loop long enough to catch
73 # any bgerrors.
74 proc tkbide {{msg "tkbide"} {msec 500}} {
75     set ::bideVar 0
76     set ::bideError ""
77     set ::bideErrorInfo ""
78     # It looks like update idletasks does the job.
79     if {0} {
80         after $msec {set ::bideVar 1}
81         tkwait variable ::bideVar
82     }
83     update idletasks
84     if {"" != $::bideError} {
85         error "$msg: $::bideError" $::bideErrorInfo
86     }
87 }
88
89
90
91 testsNeedTcl     8.6
92 testsNeedTcltest 2.2
93
94 interp alias {} type {} ::itcl::type
95 interp alias {} widget {} ::itcl::widget
96
97 # Marks tests which are only for Tk.
98 tcltest::testConstraint tk [expr {![catch {package require Tk}]}]
99
100 ::tcltest::loadTestedCommands
101 package require itcl
102
103 #-----------------------------------------------------------------------
104 # Widgets
105
106 # A widget is just a widgetadaptor with an automatically created hull
107 # component (a Tk frame).  So the widgetadaptor tests apply; all we
108 # need to test here is the frame creation.
109
110 test widget-1.1 {creating a widget
111 } -constraints {
112     tk
113 } -body {
114     widget myframe {
115         delegate method * to itcl_hull
116         delegate option * to itcl_hull
117     }
118
119     myframe create .frm -background green
120
121     set a [.frm cget -background]
122     set b [.frm itcl_hull]
123
124     destroy .frm
125     tkbide
126     list $a $b
127 } -cleanup {
128     myframe destroy
129 } -result {green ::itcl::internal::widgets::hull1.frm}
130
131 test widget-2.1 {can't redefine hull
132 } -constraints {
133     tk
134 } -body {
135     # there is no need to define or set itcl_hull as that is done automatically
136     widget myframe {
137         method resethull {} {
138         set itcl_hull ""
139         }
140     }
141
142     myframe .frm
143
144     .frm resethull
145 } -returnCodes {
146     error
147 } -cleanup {
148     myframe destroy
149 } -result {can't set "itcl_hull": The itcl_hull component cannot be redefined}
150
151
152 #-----------------------------------------------------------------------
153 # install
154 #
155 # The install command is used to install widget components, while getting
156 # options for the option database.
157
158 test install-1.1 {installed components are created properly
159 } -constraints {
160     tk
161 } -body {
162     widget myframe {
163         # Delegate an option just to make sure the component variable
164         # exists.
165         delegate option -font to text
166
167         constructor {args} {
168             installcomponent text using text $win.text -background green
169         }
170
171         method getit {} {
172             $win.text cget -background
173         }
174     }
175
176     myframe .frm
177     set a [.frm getit]
178     destroy .frm
179     tkbide
180     set a
181 } -cleanup {
182     myframe destroy
183 } -result {green}
184
185 test install-1.2 {installed components are saved properly
186 } -constraints {
187     tk
188 } -body {
189     widget myframe {
190         # Delegate an option just to make sure the component variable
191         # exists.
192         delegate option -font to text
193
194         constructor {args} {
195             installcomponent text using text $win.text -background green
196         }
197
198         method getit {} {
199             $text cget -background
200         }
201     }
202
203     myframe .frm
204     set a [.frm getit]
205     destroy .frm
206     tkbide
207     set a
208 } -cleanup {
209     myframe destroy
210 } -result {green}
211
212 test install-1.4 {install queries option database
213 } -constraints {
214     tk
215 } -body {
216     widget myframe {
217         delegate option -font to text
218
219         typeconstructor {
220             option add *Myframe.font Courier
221         }
222
223         constructor {args} {
224             installcomponent text using text $win.text
225         }
226     }
227
228     myframe .frm
229     set a [.frm cget -font]
230     destroy .frm
231     tkbide
232     set a
233 } -cleanup {
234     myframe destroy
235 } -result {Courier}
236
237 test install-1.5 {explicit options override option database
238 } -constraints {
239     tk
240 } -body {
241     widget myframe {
242         delegate option -font to text
243
244         typeconstructor {
245             option add *Myframe.font Courier
246         }
247
248         constructor {args} {
249             installcomponent text using text $win.text -font Times
250         }
251     }
252
253     myframe .frm
254     set a [.frm cget -font]
255     destroy .frm
256     tkbide
257     set a
258 } -cleanup {
259     myframe destroy
260 } -result {Times}
261
262 test install-1.6 {option db works with targetted options
263 } -constraints {
264     tk
265 } -body {
266     widget myframe {
267         delegate option -textfont to text as -font
268
269         typeconstructor {
270             option add *Myframe.textfont Courier
271         }
272
273         constructor {args} {
274             installcomponent text using text $win.text
275         }
276     }
277
278     myframe .frm
279     set a [.frm cget -textfont]
280     destroy .frm
281     tkbide
282     set a
283 } -cleanup {
284     myframe destroy
285 } -result {Courier}
286
287 test install-1.8 {install can install non-widget components
288 } -constraints {
289     tk
290 } -body {
291     type dog {
292         option -tailcolor black
293     }
294
295     widget myframe {
296         delegate option -tailcolor to thedog
297
298         typeconstructor {
299             option add *Myframe.tailcolor green
300         }
301
302         constructor {args} {
303             installcomponent thedog using dog $win.dog
304         }
305     }
306
307     myframe .frm
308     set a [.frm cget -tailcolor]
309     destroy .frm
310     tkbide
311     set a
312
313 } -cleanup {
314     dog destroy
315     myframe destroy
316 } -result {green}
317
318 test install-1.9 {ok if no options are delegated to component
319 } -constraints {
320     tk
321 } -body {
322     type dog {
323         option -tailcolor black
324     }
325
326     widget myframe {
327         constructor {args} {
328             installcomponent thedog using dog $win.dog
329         }
330     }
331
332     myframe .frm
333     destroy .frm
334     tkbide
335
336     # Test passes if no error is raised.
337     list ok
338 } -cleanup {
339     myframe destroy
340     dog destroy
341 } -result {ok}
342
343 test install-2.1 {
344     delegate option * for a non-shadowed option.  The text widget's
345     -foreground and -font options should be set according to what's
346     in the option database on the widgetclass.
347 } -constraints {
348     tk
349 } -body {
350     widget myframe {
351         delegate option * to text
352
353         typeconstructor {
354             option add *Myframe.foreground red
355             option add *Myframe.font {Times 14}
356         }
357
358         constructor {args} {
359             installcomponent text using text $win.text
360         }
361     }
362
363     myframe .frm
364     set a [.frm cget -foreground]
365     set b [.frm cget -font]
366     destroy .frm
367     tkbide
368
369     list $a $b
370 } -cleanup {
371     myframe destroy
372 } -result {red {Times 14}}
373
374
375 test install-2.2 {
376     Delegate option * for a shadowed option.  Foreground is declared
377     as a non-delegated option, hence it will pick up the option database
378     default.  -foreground is not included in the "delegate option *", so
379     the text widget's -foreground option will not be set from the
380     option database.
381 } -constraints {
382     tk
383 } -body {
384     widget myframe {
385         option -foreground white
386         delegate option * to text
387
388         typeconstructor {
389             option add *Myframe.foreground red
390         }
391
392         constructor {args} {
393             installcomponent text using text $win.text
394         }
395
396         method getit {} {
397             $text cget -foreground
398         }
399     }
400
401     myframe .frm
402     set a [.frm cget -foreground]
403     set b [.frm getit]
404     destroy .frm
405     tkbide
406
407     expr {![string equal $a $b]}
408 } -cleanup {
409     myframe destroy
410 } -result {1}
411
412 test install-2.3 {
413     Delegate option * for a creation option.  Because the text widget's
414     -foreground is set explicitly by the constructor, that always
415     overrides the option database.
416 } -constraints {
417     tk
418 } -body {
419     widget myframe {
420         delegate option * to text
421
422         typeconstructor {
423             option add *Myframe.foreground red
424         }
425
426         constructor {args} {
427             installcomponent text using text $win.text -foreground blue
428         }
429     }
430
431     myframe .frm
432     set a [.frm cget -foreground]
433     destroy .frm
434     tkbide
435
436     set a
437 } -cleanup {
438     myframe destroy
439 } -result {blue}
440
441 test install-2.4 {
442     Delegate option * with an excepted option.  Because the text widget's
443     -state is excepted, it won't be set from the option database.
444 } -constraints {
445     tk
446 } -body {
447     widget myframe {
448         delegate option * to text except -state
449
450         typeconstructor {
451             option add *Myframe.foreground red
452             option add *Myframe.state disabled
453         }
454
455         constructor {args} {
456             installcomponent text using text $win.text
457         }
458
459         method getstate {} {
460             $text cget -state
461         }
462     }
463
464     myframe .frm
465     set a [.frm getstate]
466     destroy .frm
467     tkbide
468
469     set a
470 } -cleanup {
471     myframe destroy
472 } -result {normal}
473
474
475 #-----------------------------------------------------------------------
476 # Advanced installhull tests
477 #
478 # installhull is used to install the hull widget for both widgets and
479 # widget adaptors.  It has two forms.  In one form it installs a widget
480 # created by some third party; in this form no querying of the option
481 # database is needed, because we haven't taken responsibility for creating
482 # it.  But in the other form (installhull using) installhull actually
483 # creates the widget, and takes responsibility for querying the
484 # option database as needed.
485 #
486 # NOTE: "installhull using" is always used to create a widget's hull frame.
487 #
488 # That options passed into installhull override those from the
489 # option database.
490
491 test installhull-1.1 {
492     options delegated to a widget's itcl_hull frame with the same name are
493     initialized from the option database.  Note that there's no
494     explicit code in Snit to do this; it happens because we set the
495     -class when the widget was created.  In fact, it happens whether
496     we delegate the option name or not.
497 } -constraints {
498     tk
499 } -body {
500     widget myframe {
501         delegate option -background to itcl_hull
502
503         typeconstructor {
504             option add *Myframe.background red
505             option add *Myframe.width 123
506         }
507
508         method getwid {} {
509             $itcl_hull cget -width
510         }
511     }
512
513     myframe .frm
514     set a [.frm cget -background]
515     set b [.frm getwid]
516     destroy .frm
517     tkbide
518     list $a $b
519 } -cleanup {
520     myframe destroy
521 } -result {red 123}
522
523 test installhull-1.2 {
524     Options delegated to a widget's itcl_hull frame with a different name are
525     initialized from the option database.
526 } -constraints {
527     tk
528 } -body {
529     widget myframe {
530         delegate option -mainbackground to itcl_hull as -background
531
532         typeconstructor {
533             option add *Myframe.mainbackground green
534         }
535     }
536
537     myframe .frm
538     set a [.frm cget -mainbackground]
539     destroy .frm
540     tkbide
541     set a
542 } -cleanup {
543     myframe destroy
544 } -result {green}
545
546
547
548 test option-5.1 {local widget options read from option database
549 } -constraints {
550     tk
551 } -body {
552     widget dog {
553         option -foo a
554         option -bar b
555
556         typeconstructor {
557             option add *Dog.bar bb
558         }
559     }
560
561     dog .fido
562     set a [.fido cget -foo]
563     set b [.fido cget -bar]
564     destroy .fido
565     tkbide
566
567     list $a $b
568
569 } -cleanup {
570     dog destroy
571 } -result {a bb}
572
573 test option-5.2 {local option database values available in constructor
574 } -constraints {
575     tk
576 } -body {
577     widget dog {
578         option -bar b
579         variable saveit
580
581         typeconstructor {
582             option add *Dog.bar bb
583         }
584
585         constructor {args} {
586             set saveit $itcl_options(-bar)
587         }
588
589         method getit {} {
590             return $saveit
591         }
592     }
593
594     dog .fido
595     set result [.fido getit]
596     destroy .fido
597     tkbide
598
599     set result
600 } -cleanup {
601     dog destroy
602 } -result {bb}
603
604 #-----------------------------------------------------------------------
605 # Setting the widget class explicitly
606
607 test widgetclass-1.3 {widgetclass must begin with uppercase letter
608 } -constraints {
609     tk
610 } -body {
611     widget dog {
612         widgetclass dog
613     }
614 } -returnCodes {
615     error
616 } -result {widgetclass "dog" does not begin with an uppercase letter}
617
618 test widgetclass-1.4 {widgetclass can only be defined once
619 } -constraints {
620     tk
621 } -body {
622     widget dog {
623         widgetclass Dog
624         widgetclass Dog
625     }
626 } -returnCodes {
627     error
628 } -result {too many widgetclass statements}
629
630 test widgetclass-1.5 {widgetclass set successfully
631 } -constraints {
632     tk
633 } -body {
634     widget dog {
635         widgetclass DogWidget
636     }
637
638     # The test passes if no error is thrown.
639     list ok
640 } -cleanup {
641     dog destroy
642 } -result {ok}
643
644 test widgetclass-1.6 {implicit widgetclass applied to hull
645 } -constraints {
646     tk
647 } -body {
648     widget dog {
649         typeconstructor {
650             option add *Dog.background green
651         }
652
653         method background {} {
654             $itcl_hull cget -background
655         }
656     }
657
658     dog .dog
659
660     set bg [.dog background]
661
662     destroy .dog
663
664     set bg
665 } -cleanup {
666     dog destroy
667 } -result {green}
668
669 test widgetclass-1.7 {explicit widgetclass applied to hull
670 } -constraints {
671     tk
672 } -body {
673     widget dog {
674         widgetclass DogWidget
675
676         typeconstructor {
677             option add *DogWidget.background yellow
678         }
679
680         method background {} {
681             $itcl_hull cget -background
682         }
683     }
684
685     dog .dog
686
687     set bg [.dog background]
688
689     destroy .dog
690
691     set bg
692 } -cleanup {
693     dog destroy
694 } -result {yellow}
695
696 #-----------------------------------------------------------------------
697 # hulltype statement
698
699 test hulltype-1.3 {hulltype can be frame
700 } -constraints {
701     tk
702 } -body {
703     widget dog {
704         delegate option * to itcl_hull
705         hulltype frame
706     }
707
708     dog .fido
709     catch {.fido configure -use} result
710     destroy .fido
711     tkbide
712
713     set result
714 } -cleanup {
715     dog destroy
716 } -result {unknown option "-use"}
717
718 test hulltype-1.4 {hulltype can be toplevel
719 } -constraints {
720     tk
721 } -body {
722     widget dog {
723         delegate option * to itcl_hull
724         hulltype toplevel
725     }
726
727     dog .fido
728     catch {.fido configure -use} result
729     destroy .fido
730     tkbide
731
732     set result
733 } -cleanup {
734     dog destroy
735 } -result {-use use Use {} {}}
736
737 test hulltype-1.5 {hulltype can only be defined once
738 } -constraints {
739     tk
740 } -body {
741     widget dog {
742         hulltype frame
743         hulltype toplevel
744     }
745 } -returnCodes {
746     error
747 } -result {too many hulltype statements}
748
749 test hulltype-2.1 {list of valid hulltypes
750 } -constraints {
751     tk
752 } -body {
753     type dog {
754     }
755
756     lsort [dog info hulltypes]
757 } -cleanup {
758     dog destroy
759 } -result {frame labelframe toplevel ttk:frame ttk:labelframe ttk:toplevel}
760
761 test winfo-10.1 {widget info widgets
762 } -constraints {
763     tk
764 } -body {
765     widget dog {
766     }
767
768     widget cat {
769     }
770
771     lsort [dog info widgets]
772 } -cleanup {
773     dog destroy
774     cat destroy
775 } -result {cat dog}
776
777 test winfo-10.2 {widget info components
778 } -constraints {
779     tk
780 } -body {
781     widget dog {
782         component comp1
783         component comp2
784     }
785
786     widget cat {
787         component comp1
788         component comp1a
789     }
790
791     set a [lsort [dog info components]]
792     set b [lsort [cat info components]]
793     list $a $b
794 } -cleanup {
795     dog destroy
796     cat destroy
797 } -result {{comp1 comp2 itcl_hull} {comp1 comp1a itcl_hull}}
798
799 test winfo-10.3 {widget info widgetclasses
800 } -constraints {
801     tk
802 } -body {
803     widget dog {
804         widgetclass DogWidget
805     }
806
807     widget cat {
808         widgetclass CatWidget
809     }
810
811     lsort [dog info widgetclasses]
812 } -cleanup {
813     dog destroy
814     cat destroy
815 } -result {CatWidget DogWidget}
816
817
818 #---------------------------------------------------------------------
819 # Clean up
820
821 ::tcltest::cleanupTests
822 return