From 7f58e0ba4fbddabfec26b4b4cea9bd1a049affd9 Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 17 Nov 2003 00:23:40 +0000 Subject: [PATCH] * lib/test/unit/ui/tk/testrunner.rb, lib/test/unit/ui/gtk/testrunner.rb: run GUI main loop in sub thread. * lib/test/unit/ui/gtk2/testrunner.rb: imported from rough. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4970 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 + lib/test/unit/autorunner.rb | 4 + lib/test/unit/ui/gtk/testrunner.rb | 52 +++- lib/test/unit/ui/gtk2/testrunner.rb | 467 ++++++++++++++++++++++++++++ lib/test/unit/ui/tk/testrunner.rb | 50 +-- 5 files changed, 546 insertions(+), 35 deletions(-) create mode 100644 lib/test/unit/ui/gtk2/testrunner.rb diff --git a/ChangeLog b/ChangeLog index 5a547d7942..6bca979107 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Mon Nov 17 09:23:38 2003 Nobuyoshi Nakada + + * lib/test/unit/ui/tk/testrunner.rb, + lib/test/unit/ui/gtk/testrunner.rb: + run GUI main loop in sub thread. + + * lib/test/unit/ui/gtk2/testrunner.rb: imported from rough. + Sun Nov 16 18:10:57 2003 Nobuyoshi Nakada * eval.c (rb_eval): iterator should return value from next inside diff --git a/lib/test/unit/autorunner.rb b/lib/test/unit/autorunner.rb index 3f17fadcf5..5dabd7e3c0 100644 --- a/lib/test/unit/autorunner.rb +++ b/lib/test/unit/autorunner.rb @@ -25,6 +25,10 @@ module Test require 'test/unit/ui/gtk/testrunner' Test::Unit::UI::GTK::TestRunner.run(r.suite) end, + :gtk2 => proc do |r| + require 'test/unit/ui/gtk2/testrunner' + Test::Unit::UI::GTK2::TestRunner.run(r.suite) + end, :fox => proc do |r| require 'test/unit/ui/fox/testrunner' Test::Unit::UI::Fox::TestRunner.run(r.suite) diff --git a/lib/test/unit/ui/gtk/testrunner.rb b/lib/test/unit/ui/gtk/testrunner.rb index b62e76920b..7582c489a5 100644 --- a/lib/test/unit/ui/gtk/testrunner.rb +++ b/lib/test/unit/ui/gtk/testrunner.rb @@ -23,7 +23,6 @@ module Test # Creates a new TestRunner and runs the suite. def self.run(suite) new(suite).start - end # Creates a new TestRunner for running the passed @@ -34,6 +33,14 @@ module Test else @suite = suite end + + @runner = Thread.current + @restart_signal = Class.new(Exception) + @viewer = Thread.start do + @runner.join rescue @runner.run + Gtk.main + end + @viewer.join rescue nil # wait deadlock to handshake end # Begins the test run. @@ -55,24 +62,42 @@ module Test end def attach_to_mediator # :nodoc: - run_button.signal_connect("clicked", nil) { @mediator.run_suite } + run_button.signal_connect("clicked", nil, &method(:run_test)) @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(TestCase::FINISHED, &method(:test_finished)) @mediator.add_listener(TestRunnerMediator::FINISHED, &method(:finished)) end + + def run_test(*) + @runner.raise(@restart_signal) + end def start_ui # :nodoc: - timer = Gtk::timeout_add(0) { - Gtk::timeout_remove(timer) - @mediator.run_suite - } - Gtk.main + @viewer.run + running = false + begin + loop do + if (running ^= true) + run_button.child.text = "Stop" + @mediator.run_suite + else + run_button.child.text = "Run" + @viewer.join + break + end + end + rescue @restart_signal + retry + rescue + end + abort if @red end - def stop # :nodoc: + def stop(*) # :nodoc: Gtk.main_quit end @@ -113,8 +138,6 @@ module Test end def result_changed(result) # :nodoc: - test_progress_bar.set_value(test_progress_bar.get_value + 1) - run_count_label.set_text(result.run_count.to_s) assertion_count_label.set_text(result.assertion_count.to_s) failure_count_label.set_text(result.failure_count.to_s) @@ -129,6 +152,10 @@ module Test output_status("Running #{test_name}...") end + def test_finished(test_name) + test_progress_bar.set_value(test_progress_bar.get_value + 1) + end + def finished(elapsed_time) output_status("Finished in #{elapsed_time} seconds") end @@ -138,7 +165,7 @@ module Test end def setup_ui # :nodoc: - main_window.signal_connect("destroy", nil) { stop } + main_window.signal_connect("destroy", nil, &method(:stop)) main_window.show_all fault_list.signal_connect("select-child", nil) { | list, item, data | @@ -207,7 +234,8 @@ module Test def test_progress_bar # :nodoc: lazy_initialize(:test_progress_bar) { @test_progress_bar = EnhancedProgressBar.new - @test_progress_bar.set_usize(@test_progress_bar.allocation.width, 50) + @test_progress_bar.set_usize(@test_progress_bar.allocation.width, + info_panel.size_request.height) @test_progress_bar.set_style(green_style) } end diff --git a/lib/test/unit/ui/gtk2/testrunner.rb b/lib/test/unit/ui/gtk2/testrunner.rb new file mode 100644 index 0000000000..ee78f91ac8 --- /dev/null +++ b/lib/test/unit/ui/gtk2/testrunner.rb @@ -0,0 +1,467 @@ +# :nodoc: +# +# Author:: Kenta MURATA. +# Copyright:: Copyright (c) 2000-2002 Kenta MURATA. All rights reserved. +# License:: Ruby license. + +require "gtk2" +require "test/unit/ui/testrunnermediator" +require "test/unit/ui/testrunnerutilities" + +module Test + module Unit + module UI + module GTK2 # :nodoc: all + + Gtk.init + + class EnhancedLabel < Gtk::Label # :nodoc: all + def set_text(text) + super(text.gsub(/\n\t/, "\n ")) + end + end + + class FaultList < Gtk::TreeView # :nodoc: all + def initialize + @faults = [] + @model = Gtk::ListStore.new(String, String) + super(@model) + column = Gtk::TreeViewColumn.new + column.visible = false + append_column(column) + renderer = Gtk::CellRendererText.new + column = Gtk::TreeViewColumn.new("Failures", renderer, {:text => 1}) + append_column(column) + selection.mode = Gtk::SELECTION_SINGLE + set_rules_hint(true) + set_headers_visible(false) + end # def initialize + + def add_fault(fault) + @faults.push(fault) + iter = @model.append + iter.set_value(0, (@faults.length - 1).to_s) + iter.set_value(1, fault.short_display) + end # def add_fault(fault) + + def get_fault(iter) + @faults[iter.get_value(0).to_i] + end # def get_fault + + def clear + model.clear + end # def clear + end + + class TestRunner + extend TestRunnerUtilities + + def lazy_initialize(symbol) # :nodoc: + if !instance_eval("defined?(@#{symbol})") then + yield + end + return instance_eval("@#{symbol}") + end + private :lazy_initialize + + def status_entry # :nodoc: + lazy_initialize(:status_entry) do + @status_entry = Gtk::Entry.new + @status_entry.editable = false + end + end + private :status_entry + + def status_panel # :nodoc: + lazy_initialize(:status_panel) do + @status_panel = Gtk::HBox.new + @status_panel.border_width = 10 + @status_panel.pack_start(status_entry, true, true, 0) + end + end + private :status_panel + + def fault_detail_label # :nodoc: + lazy_initialize(:fault_detail_label) do + @fault_detail_label = EnhancedLabel.new("") +# style = Gtk::Style.new +# font = Gdk::Font. +# font_load("-*-Courier 10 Pitch-medium-r-normal--*-120-*-*-*-*-*-*") +# style.set_font(font) +# @fault_detail_label.style = style + @fault_detail_label.justify = Gtk::JUSTIFY_LEFT + @fault_detail_label.wrap = false + end + end + private :fault_detail_label + + def inner_detail_sub_panel # :nodoc: + lazy_initialize(:inner_detail_sub_panel) do + @inner_detail_sub_panel = Gtk::HBox.new + @inner_detail_sub_panel.pack_start(fault_detail_label, false, false, 0) + end + end + private :inner_detail_sub_panel + + def outer_detail_sub_panel # :nodoc: + lazy_initialize(:outer_detail_sub_panel) do + @outer_detail_sub_panel = Gtk::VBox.new + @outer_detail_sub_panel.pack_start(inner_detail_sub_panel, false, false, 0) + end + end + private :outer_detail_sub_panel + + def detail_scrolled_window # :nodoc: + lazy_initialize(:detail_scrolled_window) do + @detail_scrolled_window = Gtk::ScrolledWindow.new + @detail_scrolled_window.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC) + @detail_scrolled_window. + set_size_request(400, @detail_scrolled_window.allocation.height) + @detail_scrolled_window.add_with_viewport(outer_detail_sub_panel) + end + end + private :detail_scrolled_window + + def detail_panel # :nodoc: + lazy_initialize(:detail_panel) do + @detail_panel = Gtk::HBox.new + @detail_panel.border_width = 10 + @detail_panel.pack_start(detail_scrolled_window, true, true, 0) + end + end + private :detail_panel + + def fault_list # :nodoc: + lazy_initialize(:fault_list) do + @fault_list = FaultList.new + end + end + private :fault_list + + def list_scrolled_window # :nodoc: + lazy_initialize(:list_scrolled_window) do + @list_scrolled_window = Gtk::ScrolledWindow.new + @list_scrolled_window.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC) + @list_scrolled_window. + set_size_request(@list_scrolled_window.allocation.width, 150) + @list_scrolled_window.add_with_viewport(fault_list) + end + end + private :list_scrolled_window + + def list_panel # :nodoc: + lazy_initialize(:list_panel) do + @list_panel = Gtk::HBox.new + @list_panel.border_width = 10 + @list_panel.pack_start(list_scrolled_window, true, true, 0) + end + end + private :list_panel + + def error_count_label # :nodoc: + lazy_initialize(:error_count_label) do + @error_count_label = Gtk::Label.new("0") + @error_count_label.justify = Gtk::JUSTIFY_LEFT + end + end + private :error_count_label + + def failure_count_label # :nodoc: + lazy_initialize(:failure_count_label) do + @failure_count_label = Gtk::Label.new("0") + @failure_count_label.justify = Gtk::JUSTIFY_LEFT + end + end + private :failure_count_label + + def assertion_count_label # :nodoc: + lazy_initialize(:assertion_count_label) do + @assertion_count_label = Gtk::Label.new("0") + @assertion_count_label.justify = Gtk::JUSTIFY_LEFT + end + end + private :assertion_count_label + + def run_count_label # :nodoc: + lazy_initialize(:run_count_label) do + @run_count_label = Gtk::Label.new("0") + @run_count_label.justify = Gtk::JUSTIFY_LEFT + end + end + private :run_count_label + + def info_panel # :nodoc: + lazy_initialize(:info_panel) do + @info_panel = Gtk::HBox.new(false, 0) + @info_panel.border_width = 10 + @info_panel.pack_start(Gtk::Label.new("Runs:"), false, false, 0) + @info_panel.pack_start(run_count_label, true, false, 0) + @info_panel.pack_start(Gtk::Label.new("Assertions:"), false, false, 0) + @info_panel.pack_start(assertion_count_label, true, false, 0) + @info_panel.pack_start(Gtk::Label.new("Failures:"), false, false, 0) + @info_panel.pack_start(failure_count_label, true, false, 0) + @info_panel.pack_start(Gtk::Label.new("Errors:"), false, false, 0) + @info_panel.pack_start(error_count_label, true, false, 0) + end + end # def info_panel + private :info_panel + + def green_style # :nodoc: + lazy_initialize(:green_style) do + @green_style = Gtk::Style.new + @green_style.set_bg(Gtk::STATE_PRELIGHT, 0x0000, 0xFFFF, 0x0000) + end + end # def green_style + private :green_style + + def red_style # :nodoc: + lazy_initialize(:red_style) do + @red_style = Gtk::Style.new + @red_style.set_bg(Gtk::STATE_PRELIGHT, 0xFFFF, 0x0000, 0x0000) + end + end # def red_style + private :red_style + + def test_progress_bar # :nodoc: + lazy_initialize(:test_progress_bar) { + @test_progress_bar = Gtk::ProgressBar.new + @test_progress_bar.fraction = 0.0 + @test_progress_bar. + set_size_request(@test_progress_bar.allocation.width, + info_panel.size_request[1]) + @test_progress_bar.style = green_style + } + end # def test_progress_bar + private :test_progress_bar + + def progress_panel # :nodoc: + lazy_initialize(:progress_panel) do + @progress_panel = Gtk::HBox.new(false, 10) + @progress_panel.border_width = 10 + @progress_panel.pack_start(test_progress_bar, true, true, 0) + end + end # def progress_panel + + def run_button # :nodoc: + lazy_initialize(:run_button) do + @run_button = Gtk::Button.new("Run") + end + end # def run_button + + def suite_name_entry # :nodoc: + lazy_initialize(:suite_name_entry) do + @suite_name_entry = Gtk::Entry.new + @suite_name_entry.editable = false + end + end # def suite_name_entry + private :suite_name_entry + + def suite_panel # :nodoc: + lazy_initialize(:suite_panel) do + @suite_panel = Gtk::HBox.new(false, 10) + @suite_panel.border_width = 10 + @suite_panel.pack_start(Gtk::Label.new("Suite:"), false, false, 0) + @suite_panel.pack_start(suite_name_entry, true, true, 0) + @suite_panel.pack_start(run_button, false, false, 0) + end + end # def suite_panel + private :suite_panel + + def main_panel # :nodoc: + lazy_initialize(:main_panel) do + @main_panel = Gtk::VBox.new(false, 0) + @main_panel.pack_start(suite_panel, false, false, 0) + @main_panel.pack_start(progress_panel, false, false, 0) + @main_panel.pack_start(info_panel, false, false, 0) + @main_panel.pack_start(list_panel, false, false, 0) + @main_panel.pack_start(detail_panel, true, true, 0) + @main_panel.pack_start(status_panel, false, false, 0) + end + end # def main_panel + private :main_panel + + def main_window # :nodoc: + lazy_initialize(:main_window) do + @main_window = Gtk::Window.new(Gtk::Window::TOPLEVEL) + @main_window.set_title("Test::Unit TestRunner") + @main_window.set_default_size(800, 600) + @main_window.set_resizable(true) + @main_window.add(main_panel) + end + end # def main_window + private :main_window + + def setup_ui # :nodoc: + main_window.signal_connect("destroy", nil) { stop } + main_window.show_all + fault_list.selection.signal_connect("changed", nil) do + |selection, data| + if selection.selected then + show_fault(fault_list.get_fault(selection.selected)) + else + clear_fault + end + end + end # def setup_ui + private :setup_ui + + def output_status(string) # :nodoc: + status_entry.set_text(string) + end # def output_status(string) + private :output_status + + def finished(elapsed_time) # :nodoc: + test_progress_bar.fraction = 1.0 + output_status("Finished in #{elapsed_time} seconds") + end # def finished(elapsed_time) + private :finished + + def test_started(test_name) # :nodoc: + output_status("Running #{test_name}...") + end # def test_started(test_name) + private :test_started + + def started(result) # :nodoc: + output_status("Started...") + end # def started(result) + private :started + + def test_finished(result) # :nodoc: + test_progress_bar.fraction += 1.0 / @count + end # def test_finished(result) + + def result_changed(result) # :nodoc: + run_count_label.label = result.run_count.to_s + assertion_count_label.label = result.assertion_count.to_s + failure_count_label.label = result.failure_count.to_s + error_count_label.label = result.error_count.to_s + end # def result_changed(result) + private :result_changed + + def clear_fault # :nodoc: + raw_show_fault("") + end # def clear_fault + private :clear_fault + + def raw_show_fault(string) # :nodoc: + fault_detail_label.set_text(string) + outer_detail_sub_panel.queue_resize + end # def raw_show_fault(string) + private :raw_show_fault + + def show_fault(fault) # :nodoc: + raw_show_fault(fault.long_display) + end # def show_fault(fault) + private :show_fault + + def add_fault(fault) # :nodoc: + if not @red then + test_progress_bar.style = red_style + @red = true + end + fault_list.add_fault(fault) + end # def add_fault(fault) + private :add_fault + + def reset_ui(count) # :nodoc: + test_progress_bar.style = green_style + test_progress_bar.fraction = 0.0 + @count = count + 1 + @red = false + + run_count_label.set_text("0") + assertion_count_label.set_text("0") + failure_count_label.set_text("0") + error_count_label.set_text("0") + + fault_list.clear + end # def reset_ui(count) + private :reset_ui + + def stop # :nodoc: + Gtk.main_quit + end # def stop + private :stop + + def run_test + @runner.raise(@restart_signal) + end + private :run_test + + def start_ui # :nodoc + @viewer.run + running = false + begin + loop do + if (running ^= true) + run_button.child.text = "Stop" + @mediator.run_suite + else + run_button.child.text = "Run" + @viewer.join + break + end + end + rescue @restart_signal + retry + rescue + end + abort if @red + end # def start_ui + private :start_ui + + def attach_to_mediator + run_button.signal_connect("clicked", nil) { run_test } + @mediator.add_listener(TestRunnerMediator::RESET, &method(:reset_ui)) + @mediator.add_listener(TestRunnerMediator::STARTED, &method(:started)) + @mediator.add_listener(TestRunnerMediator::FINISHED, &method(:finished)) + @mediator.add_listener(TestResult::FAULT, &method(:add_fault)) + @mediator.add_listener(TestResult::CHANGED, &method(:result_changed)) + @mediator.add_listener(TestCase::STARTED, &method(:test_started)) + @mediator.add_listener(TestCase::FINISHED, &method(:test_finished)) + end # def attach_to_mediator + private :attach_to_mediator + + def setup_mediator + @mediator = TestRunnerMediator.new(@suite) + suite_name = @suite.to_s + if @suite.kind_of?(Module) then + suite_name = @suite.name + end + suite_name_entry.set_text(suite_name) + end # def setup_mediator + private :setup_mediator + + def start + setup_mediator + setup_ui + attach_to_mediator + start_ui + end # def start + + def initialize(suite) + if suite.respond_to?(:suite) then + @suite = suite.suite + else + @suite = suite + end + + @runner = Thread.current + @restart_signal = Class.new(Exception) + @viewer = Thread.start do + @runner.join rescue @runner.run + Gtk.main + end + @viewer.join rescue nil # wait deadlock to handshake + end # def initialize(suite) + + def self.run(suite) + new(suite).start + end # def self.run(suite) + + end # class TestRunner + + end # module GTK2 + end # module UI + end # module Unit +end # module Test diff --git a/lib/test/unit/ui/tk/testrunner.rb b/lib/test/unit/ui/tk/testrunner.rb index b754bad0a2..19c39a22f8 100644 --- a/lib/test/unit/ui/tk/testrunner.rb +++ b/lib/test/unit/ui/tk/testrunner.rb @@ -24,7 +24,6 @@ module Test # Creates a new TestRunner and runs the suite. def self.run(suite) new(suite).start - end # Creates a new TestRunner for running the passed @@ -38,7 +37,13 @@ module Test @red = false @fault_detail_list = [] - @run_suite_thread = nil + @runner = Thread.current + @restart_signal = Class.new(Exception) + @viewer = Thread.start do + @runner.join rescue @runner.run + ::Tk.mainloop + end + @viewer.join rescue nil # wait deadlock to handshake end # Begins the test run. @@ -60,7 +65,7 @@ module Test end def attach_to_mediator # :nodoc: - @run_button.command(method(:run_suite)) + @run_button.command(method(:run_test)) @fault_list.bind('ButtonPress-1', proc{|y| fault = @fault_detail_list[@fault_list.nearest(y)] if fault @@ -75,24 +80,32 @@ module Test @mediator.add_listener(TestRunnerMediator::FINISHED, &method(:finished)) end + def run_test + @runner.raise(@restart_signal) + end + def start_ui # :nodoc: - run_suite + @viewer.run + running = false begin - ::Tk.mainloop - rescue Exception - if @run_suite_thread and @run_suite_thread.alive? - @run_suite_thread.raise $! - retry - else - raise + loop do + if (running ^= true) + @run_button.configure('text'=>'Stop') + @mediator.run_suite + else + @run_button.configure('text'=>'Run') + @viewer.join + break + end end + rescue @restart_signal + retry + rescue end + abort if @red end def stop # :nodoc: - if @run_suite_thread and @run_suite_thread.alive? - @run_suite_thread.kill - end ::Tk.exit end @@ -239,15 +252,6 @@ module Test TkLabel.new(parent, 'textvariable'=>v).pack('side'=>'left', 'expand'=>true) v end - - def run_suite # :nodoc: - run_proc = proc { - @run_suite_thread = Thread.start { - @mediator.run_suite - } - } - TkAfter.new(1000, 1, run_proc).start - end end end end