01: package example;
02:
03: import java.awt.event.*;
04:
05: import javax.swing.JPanel;
06:
07: import junit.extensions.abbot.*;
08: import abbot.tester.ComponentTester;
09:
10: /**
11: * Source code for Tutorial 1.
12: * Simple unit tests for example.ArrowButton. Also demonstrates the use of
13: * ComponentTestFixture.
14: */
15:
16: public class ArrowButtonTest extends ComponentTestFixture {
17:
18: private ComponentTester tester;
19:
20: protected void setUp() {
21: tester = ComponentTester.getTester(ArrowButton.class);
22: }
23:
24: private String gotClick;
25:
26: public void testClick() {
27: ActionListener al = new ActionListener() {
28: public void actionPerformed(ActionEvent ev) {
29: gotClick = ev.getActionCommand();
30: }
31: };
32:
33: ArrowButton left = new ArrowButton(ArrowButton.LEFT);
34: ArrowButton right = new ArrowButton(ArrowButton.RIGHT);
35: ArrowButton up = new ArrowButton(ArrowButton.UP);
36: ArrowButton down = new ArrowButton(ArrowButton.DOWN);
37:
38: left.addActionListener(al);
39: right.addActionListener(al);
40: up.addActionListener(al);
41: down.addActionListener(al);
42:
43: JPanel pane = new JPanel();
44: pane.add(left);
45: pane.add(right);
46: pane.add(up);
47: pane.add(down);
48: // This method provided by ComponentTestFixture
49: showFrame(pane);
50:
51: gotClick = null;
52: tester.actionClick(left);
53: assertEquals("Action failed", ArrowButton.LEFT, gotClick);
54: gotClick = null;
55: tester.actionClick(right);
56: assertEquals("Action failed", ArrowButton.RIGHT, gotClick);
57: gotClick = null;
58: tester.actionClick(up);
59: assertEquals("Action failed", ArrowButton.UP, gotClick);
60: gotClick = null;
61: tester.actionClick(down);
62: assertEquals("Action failed", ArrowButton.DOWN, gotClick);
63: }
64:
65: private int count = 0;
66:
67: public void testRepeatedFire() {
68: ArrowButton arrow = new ArrowButton(ArrowButton.LEFT);
69: ActionListener al = new ActionListener() {
70: public void actionPerformed(ActionEvent ev) {
71: ++count;
72: }
73: };
74: arrow.addActionListener(al);
75: showFrame(arrow);
76:
77: // Hold the button down for 5 seconds
78: tester.mousePress(arrow);
79: tester.actionDelay(5000);
80: tester.mouseRelease();
81: assertTrue("Didn't get any repeated events", count > 1);
82: }
83:
84: public ArrowButtonTest(String name) {
85: super (name);
86: }
87:
88: public static void main(String[] args) {
89: TestHelper.runTests(args, ArrowButtonTest.class);
90: }
91: }
|