Example 4: GUIs and Testing

Topic Building GUIs and Testing
Course CS2114
Level intermediate to advanced
Language Java, Web-CAT
More Info...
  • Slides on GUIs and events
  • GUI testing examples (zip)
Brief Description
This topic is done at the end of CS1114 or (more typically) in CS2114. We cover Java Swing judiciously, but emphasis continues to be on testing using LIFT.

Back to Menu

Graphical User Interfaces

  • Introduce event driven programming to students
  • Make programs that are interesting and relevant
  • But how much of Swing to you teach? Not much...
  • You can use Swing, JTF or objectdraw...
  • But what about testing? Lets look...

Push Counter Example

  • Button increments a counter
  • Button is embedded in a panel that is self contained
  • Main program creates a window, puts the panel in it and makes it visible
    // Main
    JFrame frame = new JFrame("Push Counter");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    frame.getContentPane().add(new PushCounterPanel());
    frame.pack();
    frame.setVisible(true);
    

PushCounterPanel extends JPanel

Instance variable "count" and internal class ButtonListener

push = new JButton("Push Me!");
push.setName("button");
push.addActionListener(new ButtonListener());

label = new JLabel("Pushes: " + count);
label.setName("count");

add(push);
add(label);

setPreferredSize(new Dimension(300, 40));
setBackground(Color.cyan);

Internal class ButtonListener

Internal class makes it easy to update counter from listener

public void actionPerformed(ActionEvent event)
{
    count++;
    label.setText("Pushes: " + count);
}

Test your main program

  • This main is so simple, that just one test is enough
    public void testPushCounter()
    {
        // You can test complete drivers just by
        // invoking main()
        PushCounter.main(null);
    
       // You can still retrieve components by name
        JButton pushMe = getComponent(JButton.class, "button");
        assertNotNull(pushMe);
    
        // You can even retrieve the panel that is visible
        PushCounterPanel panel = getComponent(PushCounterPanel.class);
        assertNotNull(panel);
    }
    

Testing the PushCounterPanel is more intersting

public void testPushCounterPanel()
{
    // You can test individual panels by themselves
    PushCounterPanel panel = new PushCounterPanel();
    showInFrame(panel);

    // You can retrieve components by name
    JButton pushMe = getComponent(JButton.class, "button");
    JLabel count = getComponent(JLabel.class, "count");

    // You can invoke actions:
    click(pushMe);
    click(pushMe);

    // And check the state of components
    assertEquals("Pushes: 2", count.getText());
}

LIFT library

This is our library that allows students to easily test a GUI.

import student.*;
public void MyTestCase extends GUITestCase {
	...
}
  • showInFrame() - creates a frame for testing and installs individual panel
  • getComponent() - gets component from the testing panel

Component Filters

Component getComponent(class, where.filter)
  Example: where.locationIs(35, 180).and.textIs("Label")

Other examples

nameIs (String name)
textIs (String text)
hasFocusIs (boolean focus)
enabledIs (boolean enabled)
typeIs (Class type)
widthIs (int width) or heightIs (int height)
sizeIs (int width, int height) or sizeIsWithin (int width, int height)
xLocationIs (int x) or yLocationIs (int y)
locationIs (int x, int y)
isLocatedWithin (Rectangle region)
isContainedWithin (Rectangle egion)

Simulating Mouse Events

Most of these methods take a Component parameter along with coordinates, and act relative to the Component.

click (Component c) or click (Component c, int x, int y)
doubleClick (Component c) or doubleClick (Component c, int x, int y)
rightClick (Component c) or rightClick (Component c, int x, int y)
click (Component c, int x, int y, int buttons, int count)
mousePress (Component c, int x, int y)
mousePress (Component c, int x, int y, int buttons)
mouseRelease ()
mouseMove (Component c, int x, int y)
mouseExit (Component c)
mouseDragFrom (Component c, int x, int y)
mouseDragFrom (Component c, int x, int y, int buttons)
mouseDragOver (Component c, int x, int y)
mouseDropOn (Component c)
mouseDropOn (Component c, int x, int y)
focus (Component c)

Simulating Keyboard Events

GUITestCase also provides the following methods to simulate keyboard events

enterText (JTextComponent jtc, String text)
keyStroke (Component c, int keyCode)
keystroke (Component c, int keyCode, int modifiers)
keyPress (Component c, int keyCode)
keyRelease (Component c, int keyCode)
keyString (Component c, String text)

Convenience Methods

GUITestCase also provides the following convenience methods for some common GUI elements

selectItem (JComboBox jcb, String item)
selectItem (JList jl, String item)
getContents (JComboBox jcb)
getContents (JList jl)
selectMenuItem (JMenuItem jmi)

RubberLine example

Look at code, slightly different from example 1.

Back to Menu