summaryrefslogtreecommitdiff
path: root/trunk/lib/test/unit/ui/fox/testrunner.rb
blob: 34a8ff1288876b1a468adf2087dbcce16993b961 (plain)
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
#--
#
# Author:: Nathaniel Talbott.
# Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
# License:: Ruby license.

require 'fox'
require 'test/unit/ui/testrunnermediator'
require 'test/unit/ui/testrunnerutilities'

include Fox

module Test
  module Unit
    module UI
      module Fox # :nodoc:

        # Runs a Test::Unit::TestSuite in a Fox UI. Obviously,
        # this one requires you to have Fox
        # (http://www.fox-toolkit.org/fox.html) and the Ruby
        # Fox extension (http://fxruby.sourceforge.net/)
        # installed.
        class TestRunner

          extend TestRunnerUtilities
          
          RED_STYLE = FXRGBA(0xFF,0,0,0xFF) #0xFF000000
          GREEN_STYLE = FXRGBA(0,0xFF,0,0xFF) #0x00FF0000

          # Creates a new TestRunner for running the passed
          # suite.
          def initialize(suite, output_level = NORMAL)
            if (suite.respond_to?(:suite))
              @suite = suite.suite
            else
              @suite = suite
            end

            @result = nil
            @red = false
          end
  
          # Begins the test run.
          def start
            setup_ui
            setup_mediator
            attach_to_mediator
            start_ui
            @result
          end

          def setup_mediator # :nodoc:
            @mediator = TestRunnerMediator.new(@suite)
            suite_name = @suite.to_s
            if ( @suite.kind_of?(Module) )
              suite_name = @suite.name
            end
            @suite_name_entry.text = suite_name
          end
          
          def attach_to_mediator # :nodoc:
            @mediator.add_listener(TestRunnerMediator::RESET, &method(:reset_ui))
            @mediator.add_listener(TestResult::FAULT, &method(:add_fault))
            @mediator.add_listener(TestResult::CHANGED, &method(:result_changed))
            @mediator.add_listener(TestRunnerMediator::STARTED, &method(:started))
            @mediator.add_listener(TestCase::STARTED, &method(:test_started))
            @mediator.add_listener(TestRunnerMediator::FINISHED, &method(:finished))
          end
  
          def start_ui # :nodoc:
            @application.create
            @window.show(PLACEMENT_SCREEN)
            @application.addTimeout(1) do
              @mediator.run_suite
            end
            @application.run
          end
          
          def stop # :nodoc:
            @application.exit(0)
          end
          
          def reset_ui(count) # :nodoc:
            @test_progress_bar.barColor = GREEN_STYLE
            @test_progress_bar.total = count
            @test_progress_bar.progress = 0
            @red = false
  
            @test_count_label.text = "0"
            @assertion_count_label.text = "0"
            @failure_count_label.text = "0"
            @error_count_label.text = "0"
  
            @fault_list.clearItems
          end
          
          def add_fault(fault) # :nodoc:
            if ( ! @red )
              @test_progress_bar.barColor = RED_STYLE
              @red = true
            end
            item = FaultListItem.new(fault)
            @fault_list.appendItem(item)
          end
          
          def show_fault(fault) # :nodoc:
            raw_show_fault(fault.long_display)
          end
          
          def raw_show_fault(string) # :nodoc:
            @detail_text.setText(string)
          end
          
          def clear_fault # :nodoc:
            raw_show_fault("")
          end
          
          def result_changed(result) # :nodoc:
            @test_progress_bar.progress = result.run_count
  
            @test_count_label.text = result.run_count.to_s
            @assertion_count_label.text = result.assertion_count.to_s
            @failure_count_label.text = result.failure_count.to_s
            @error_count_label.text = result.error_count.to_s

             # repaint now!
             @info_panel.repaint
             @application.flush
          end
          
          def started(result) # :nodoc:
            @result = result
            output_status("Started...")
          end
          
          def test_started(test_name)
            output_status("Running #{test_name}...")
          end
          
          def finished(elapsed_time)
            output_status("Finished in #{elapsed_time} seconds")
          end
          
          def output_status(string)
            @status_entry.text = string
            @status_entry.repaint
          end
  
          def setup_ui # :nodoc:
            @application = create_application
            create_tooltip(@application)

            @window = create_window(@application)
            
            @status_entry = create_entry(@window)
            
            main_panel = create_main_panel(@window)
            
            suite_panel = create_suite_panel(main_panel)
            create_label(suite_panel, "Suite:")
            @suite_name_entry = create_entry(suite_panel)
            create_button(suite_panel, "&Run\tRun the current suite", proc { @mediator.run_suite })
            
            @test_progress_bar = create_progress_bar(main_panel)
            
            @info_panel = create_info_panel(main_panel)
            create_label(@info_panel, "Tests:")
            @test_count_label = create_label(@info_panel, "0")
            create_label(@info_panel, "Assertions:")
            @assertion_count_label = create_label(@info_panel, "0")
            create_label(@info_panel, "Failures:")
            @failure_count_label = create_label(@info_panel, "0")
            create_label(@info_panel, "Errors:")
            @error_count_label = create_label(@info_panel, "0")
            
            list_panel = create_list_panel(main_panel)
            @fault_list = create_fault_list(list_panel)
            
            detail_panel = create_detail_panel(main_panel)
            @detail_text = create_text(detail_panel)
          end
          
          def create_application # :nodoc:
            app = FXApp.new("TestRunner", "Test::Unit")
            app.init([])
            app
          end
          
          def create_window(app)
            FXMainWindow.new(app, "Test::Unit TestRunner", nil, nil, DECOR_ALL, 0, 0, 450)
          end
          
          def create_tooltip(app)
            FXTooltip.new(app)
          end
  
          def create_main_panel(parent) # :nodoc:
            panel = FXVerticalFrame.new(parent, LAYOUT_FILL_X | LAYOUT_FILL_Y)
            panel.vSpacing = 10
            panel
          end
  
          def create_suite_panel(parent) # :nodoc:
            FXHorizontalFrame.new(parent, LAYOUT_SIDE_LEFT | LAYOUT_FILL_X)
          end
          
          def create_button(parent, text, action) # :nodoc:
            FXButton.new(parent, text).connect(SEL_COMMAND, &action)
          end
          
          def create_progress_bar(parent) # :nodoc:
            FXProgressBar.new(parent, nil, 0, PROGRESSBAR_NORMAL | LAYOUT_FILL_X)
          end
          
          def create_info_panel(parent) # :nodoc:
            FXMatrix.new(parent, 1, MATRIX_BY_ROWS | LAYOUT_FILL_X)
          end
          
          def create_label(parent, text)
            FXLabel.new(parent, text, nil, JUSTIFY_CENTER_X | LAYOUT_FILL_COLUMN)
          end
          
          def create_list_panel(parent) # :nodoc:
            FXHorizontalFrame.new(parent, LAYOUT_FILL_X | FRAME_SUNKEN | FRAME_THICK)
          end
          
          def create_fault_list(parent) # :nodoc:
            list = FXList.new(parent, 10, nil, 0, LIST_SINGLESELECT | LAYOUT_FILL_X) #, 0, 0, 0, 150)
            list.connect(SEL_COMMAND) do |sender, sel, ptr|
              if sender.retrieveItem(sender.currentItem).selected?
                show_fault(sender.retrieveItem(sender.currentItem).fault)
              else
                clear_fault
              end
            end
            list
          end
          
          def create_detail_panel(parent) # :nodoc:
            FXHorizontalFrame.new(parent, LAYOUT_FILL_X | LAYOUT_FILL_Y | FRAME_SUNKEN | FRAME_THICK)
          end
          
          def create_text(parent) # :nodoc:
            FXText.new(parent, nil, 0, TEXT_READONLY | LAYOUT_FILL_X | LAYOUT_FILL_Y)
          end
          
          def create_entry(parent) # :nodoc:
            entry = FXTextField.new(parent, 30, nil, 0, TEXTFIELD_NORMAL | LAYOUT_SIDE_BOTTOM | LAYOUT_FILL_X)
            entry.disable
            entry
          end
        end
  
        class FaultListItem < FXListItem # :nodoc: all
          attr_reader(:fault)
          def initialize(fault)
            super(fault.short_display)
            @fault = fault
          end
        end
      end
    end
  end
end

if __FILE__ == $0
  Test::Unit::UI::Fox::TestRunner.start_command_line_test
end