From: Phlip Date: 2004-03-18T09:19:34+09:00 Subject: Re: GUI testing on unix Simon Strandgaard wrote: > I use fxruby ;-) Here's how far I got with a quick experiment in that totally proper GUI Toolkit. Note I'm not asking how to finish this example. I hardly bothered to research it. But it illustrates the "maybe mainloop" pattern for RubyFox. #!/usr/bin/env ruby require "fox" include Fox def assert(q) puts "this app broke" if not q end class HelloWorld def initialize() @application = FXApp.new("Hello", "FoxTest") @main = FXMainWindow.new( @application, "Hello", nil, nil, DECOR_ALL ) FXButton.new( @main, "&Hello, World!", nil, @application, FXApp::ID_QUIT ) @application.create() @main.show(PLACEMENT_SCREEN) end attr :main def show() @application.run() end end def test_printHelloWorld() hello = HelloWorld.new() button = hello.main.children[0] assert "Hello, World!" == button.getText # hello.show() end def test_clickButton() hello = HelloWorld.new() button = hello.main.children[0] button.onEnter # TODO put assertion here that proves # onEnter did the right thing end def main() test_printHelloWorld() test_clickButton() end main() That program is the hello.rbw file from the Pragmatic Programmer's Installer's RubyFox sample folder, converted into a test rig for a hypothetically brainless class called HelloWorld. But it illustrates important test topics. I did not bother to throw in a test-rig, because the only features we need were assert(). The test works by promising Fox that we are just about to display a window, but only if Fox works with us and gets ready to display it. Then we throw the window away. The first test, test_printHelloWorld(), simulates looking at the window, and querying its visual state. We expect a button containing that astounding statement, "Hello, World!". But because we don't call hello.show(), the window does not flash while the test runs. The second test, test_clickButton(), is incomplete. Firstly, it only calls the callback that would have called if we had clicked on the button. I don't know if the FXButton class has a reliable Click() method here, to get more abstract. Secondly, that test aborts the test run, due to the FXApp::ID_QUIT flag. Just a little research is required to put a mock event handler in place, to assert true if our test clicked the button, and if it were properly wired. All GUI Toolkits should support testing themselves directly, in code, as efficiently as possible. Most contain many of the primitives required. But not all make how to do this easy, or obvious. -- Phlip http://www.xpsd.org/cgi-bin/wiki?TestFirstUserInterfaces