source: trunk/tester/scripts/testview.tcl @ 2143

Last change on this file since 2143 was 2083, checked in by braffert, 13 years ago

tester: status message missing case when some tests passed and some need to run.

File size: 10.5 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: testview - display the results of a test
3#
4#  Top part of right hand side of the regression tester.  Displays an
5#  overview of selected test cases, and offers a button for running
6#  them.
7# ======================================================================
8#  AUTHOR:  Ben Rafferty, Purdue University
9#           Michael McLennan, Purdue University
10#  Copyright (c) 2010-2011  Purdue Research Foundation
11#
12#  See the file "license.terms" for information on usage and
13#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14# ======================================================================
15package require Itk
16package require BLT
17
18namespace eval Rappture::Tester::TestView { #forward declaration }
19
20option add *TestView.font {Arial -12} widgetDefault
21option add *TestView.titleFont {Arial -18 bold} widgetDefault
22option add *TestView.statusFont {Arial -12 italic} widgetDefault
23option add *TestView.statusPassColor black widgetDefault
24option add *TestView.statusFailColor red widgetDefault
25option add *TestView*descScroller.width 3i widgetDefault
26option add *TestView*descScroller.height 1i widgetDefault
27
28itcl::class Rappture::Tester::TestView {
29    inherit itk::Widget
30
31    itk_option define -statuspasscolor statusPassColor StatusColor ""
32    itk_option define -statusfailcolor statusFailColor StatusColor ""
33    itk_option define -runcommand runCommand RunCommand ""
34
35    constructor {args} { #defined later }
36    public method show {args}
37
38    private method _doRun {}
39    private method _plural {n}
40
41    private variable _testobjs ""  ;# objects being displayed
42}
43
44# ----------------------------------------------------------------------
45# CONSTRUCTOR
46# ----------------------------------------------------------------------
47itcl::body Rappture::Tester::TestView::constructor {args} {
48
49    # run button to run selected tests
50    itk_component add run {
51        button $itk_interior.run -text "Run" -padx 12 -pady 12 \
52            -command [itcl::code $this _doRun]
53    }
54    pack $itk_component(run) -side right -anchor n
55
56    # shows the big icon for test: pass/fail
57    itk_component add statusicon {
58        label $itk_interior.sicon
59    }
60    pack $itk_component(statusicon) -side left -anchor n
61
62    # shows the name of the test
63    itk_component add title {
64        label $itk_interior.title -anchor w
65    } {
66        usual
67        rename -font -titlefont titleFont Font
68    }
69    pack $itk_component(title) -side top -anchor w
70
71    # shows the status of the test: pass/fail
72    itk_component add statusmesg {
73        label $itk_interior.smesg -anchor w
74    } {
75        usual
76        rename -font -statusfont statusFont Font
77        ignore -foreground
78    }
79    pack $itk_component(statusmesg) -side top -anchor w
80
81    # for the longer text describing the test
82    itk_component add descScroller {
83        Rappture::Scroller $itk_interior.descScroller \
84            -xscrollmode auto -yscrollmode auto
85    }
86    pack $itk_component(descScroller) -expand yes -fill both
87
88    itk_component add description {
89        text $itk_component(descScroller).desc -height 1 \
90            -wrap word -relief flat
91    }
92    $itk_component(descScroller) contents $itk_component(description)
93
94    eval itk_initialize $args
95}
96
97itk::usual TestView {
98    keep -background -foreground -cursor
99    keep -font -titlefont -statusfont
100}
101
102# ----------------------------------------------------------------------
103# USAGE: show <testObj> <testObj> ...
104#
105# Loads one or more Test objects into the display.  When a single
106# object is shown, we can display more detail.  When several objects
107# are shown, we provide overview info.
108# ----------------------------------------------------------------------
109itcl::body Rappture::Tester::TestView::show {args} {
110    foreach obj $args {
111        if {[catch {$obj isa Test} valid] || !$valid} {
112            error "bad value \"$obj\": should be Test object"
113        }
114    }
115    set _testobjs $args
116
117    switch -- [llength $_testobjs] {
118        0 {
119            # If an empty string is passed in then clear everything
120            $itk_component(title) configure -text ""
121            $itk_component(statusicon) configure -image ""
122            $itk_component(statusmesg) configure -text ""
123            $itk_component(description) configure -state normal
124            $itk_component(description) delete 1.0 end
125            $itk_component(description) configure -state disabled
126            $itk_component(run) configure -state disabled
127        }
128        1 {
129            set obj [lindex $_testobjs 0]
130            switch [$obj getResult] {
131                ? {
132                    set smesg "Ready to run"
133                    set sicon [Rappture::icon test64]
134                    set color $itk_option(-statuspasscolor)
135                }
136                Pass {
137                    set smesg "Test passed"
138                    set sicon [Rappture::icon pass64]
139                    set color $itk_option(-statuspasscolor)
140                }
141                Fail {
142                    set smesg "Test failed"
143                    set sicon [Rappture::icon fail64]
144                    set color $itk_option(-statusfailcolor)
145                }
146                Running - Waiting {
147                    set smesg "Test waiting to run"
148                    set sicon [Rappture::icon wait64]
149                    set color $itk_option(-statuspasscolor)
150                }
151                default { error "unknown test state \"[$obj getResult]\"" }
152            }
153            set name [lindex [split [$obj getTestInfo test.label] |] end]
154            $itk_component(title) configure -text "Test: $name"
155            $itk_component(statusicon) configure -image $sicon
156            $itk_component(statusmesg) configure -text $smesg -foreground $color
157            $itk_component(description) configure -state normal
158            $itk_component(description) delete 1.0 end
159            set desc [string trim [$obj getTestInfo test.description]]
160            if {$desc eq ""} {
161                set desc "--"
162            }
163            $itk_component(description) insert 1.0 $desc
164            $itk_component(description) configure -state disabled
165            $itk_component(run) configure -state normal
166        }
167        default {
168            array set states { ? 0  Pass 0  Fail 0  Running 0  Waiting 0  total 0 }
169            foreach obj $_testobjs {
170                incr states(total)
171                incr states([$obj getResult])
172            }
173            if {$states(total) == 1} {
174                set thistest "This test"
175            } else {
176                set thistest "These tests"
177            }
178
179            $itk_component(title) configure \
180                -text [string totitle [_plural $states(total)]]
181
182            switch -glob -- $states(Pass)/$states(Fail)/$states(?) {
183                0/0/* {
184                    set smesg "Ready to run"
185                    set sicon [Rappture::icon test64]
186                    set color $itk_option(-statuspasscolor)
187                    set desc ""
188                }
189                */0/0 {
190                    set smesg "$thistest passed"
191                    set sicon [Rappture::icon pass64]
192                    set color $itk_option(-statuspasscolor)
193                    set desc ""
194                }
195                0/*/0 {
196                    set smesg "$thistest failed"
197                    set sicon [Rappture::icon fail64]
198                    set color $itk_option(-statusfailcolor)
199                    set desc ""
200                }
201                0/*/* {
202                    if {$states(Fail) == 1} {
203                        set smesg "One of these tests failed"
204                    } else {
205                        set smesg "Some of these tests failed"
206                    }
207                    set sicon [Rappture::icon fail64]
208                    set color $itk_option(-statusfailcolor)
209                    set desc "[_plural $states(Fail)] failed\n[_plural $states(?)] need to run"
210                }
211                */*/0 {
212                    if {$states(Pass) == 1} {
213                        set smesg "One of these tests passed"
214                    } else {
215                        set smesg "Some of these tests passed"
216                    }
217                    set sicon [Rappture::icon test64]
218                    set color $itk_option(-statuspasscolor)
219                    set desc "[_plural $states(Fail)] failed\n[_plural $states(Pass)] passed"
220                }
221                */0/* {
222                    if {$states(Pass) == 1} {
223                        set smesg "One of these tests passed"
224                    } else {
225                        set smesg "Some of these tests passed"
226                    }
227                    set sicon [Rappture::icon pass64]
228                    set color $itk_option(-statuspasscolor)
229                    set desc "[_plural $states(Pass)] passed\n[_plural $states(?)] need to run"
230                }
231                default {
232                    set smesg "Some tests passed, some failed"
233                    set sicon [Rappture::icon fail64]
234                    set color $itk_option(-statusfailcolor)
235                    set desc "[_plural $states(Fail)] failed\n[_plural $states(Pass)] passed\n[_plural $states(?)] need to run"
236                }
237            }
238            $itk_component(statusicon) configure -image $sicon
239            $itk_component(statusmesg) configure -text $smesg -foreground $color
240            $itk_component(description) configure -state normal
241            $itk_component(description) delete 1.0 end
242            $itk_component(description) insert end $desc
243            $itk_component(description) configure -state disabled
244            $itk_component(run) configure -state normal
245        }
246    }
247}
248
249# ----------------------------------------------------------------------
250# USAGE: _doRun
251#
252# Invoked when the user presses the "Run" button to invoke the
253# -runcommand script for this widget.  Invokes the command with
254# the current test objects appended as arguments.
255# ----------------------------------------------------------------------
256itcl::body Rappture::Tester::TestView::_doRun {} {
257    if {[string length $itk_option(-runcommand)] > 0} {
258        uplevel #0 $itk_option(-runcommand) $_testobjs
259    }
260}
261
262# ----------------------------------------------------------------------
263# USAGE: _plural <num>
264#
265# Handy way of generating a plural string.  If <num> is 1, it returns
266# "1 test".  Otherwise it returns "<num> tests".
267# ----------------------------------------------------------------------
268itcl::body Rappture::Tester::TestView::_plural {num} {
269    if {$num == 1} {
270        return "1 test"
271    } else {
272        return "$num tests"
273    }
274}
Note: See TracBrowser for help on using the repository browser.