Java Reference
In-Depth Information
8.6.4
Test
The test of a graphical user interface is a very complex subject; here we
describe a simple approach. The test class
TestGUI
contains the test method
testSimple()
that applies the same test case used in the previous prototype.
The test checks the text in the HTML pane using the same techniques as the
previous prototypes. Since the
JEditorPane
can modify the text introducing
or removing spaces, special care must be devoted to avoid spaces in the
expected substrings.
public class
TestGUI
extends
TestCase {
//...
public void
testSimple(){
String source
#
"// this is a test class...\n"
!
"class TestClass {\n"
!
" String an_attribute;\n"
!
" public int a_public_attribute;\n"
!
" public void a_method(int a_parameter){\n"
!
" String s;\n"
!
" s
#
\"string content\"; }\n"
!
" private int another_method() { return 0; }\n"
!
"}\n";
// creates the window
TestFrame window
#
new
TestFrame();
// activate it
window.setVisible(
true
);
// set the source code
window.sourceArea.setText(source);
// simulate a click on the button
window.button.doClick();
// get the text from the htmlPane
String result
#
window.htmlPane.getText();
// destroy the window
window.dispose();
String[] expected
#
{
"<HTML>",
"<head>","<title>","Class: TestClass",
"</title>","</head>",
"<BODY","<H1>",
"<FONT","color
#
\"navy\"","TestClass","</FONT>",
"</H1>",
"<FONT","color
#
\"navy\"","Public members:","</FONT>",
"<UL>",
"<LI>","<FONT","color
#
\"green\"",
"int","a_public_attribute",
"</FONT>",