01: package abbot.tester;
02:
03: import java.awt.event.*;
04:
05: import javax.swing.*;
06:
07: import junit.extensions.abbot.*;
08:
09: /** Unit test to verify basic key stroke mappings. <p>
10: */
11:
12: public class KeyStrokeMapTest extends ComponentTestFixture {
13:
14: private JTextField tf;
15: private Robot robot;
16:
17: /** Create a new test case with the given name. */
18: public KeyStrokeMapTest(String name) {
19: super (name);
20: }
21:
22: protected void setUp() {
23: tf = new JTextField();
24: tf.setColumns(50);
25: robot = getRobot();
26: }
27:
28: private static final String charList = " !\"#$%&'()*+,-./0123456789:;<=>?@"
29: + "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
30: + "`abcdefghijklmnopqrstuvwxyz{|}~";
31:
32: /** Test the basic ASCII set. */
33: public void testCharacterGeneration() {
34: showFrame(tf);
35: robot.focus(tf);
36: robot.waitForIdle();
37: robot.keyString(charList);
38: robot.waitForIdle();
39: assertEquals("Not all ascii characters were produced properly",
40: charList, tf.getText());
41: }
42:
43: public void testBackSpace() {
44: showFrame(tf);
45: robot.focus(tf);
46: robot.waitForIdle();
47: robot.keyString("HELO\bLO");
48: robot.waitForIdle();
49: assertEquals("Backspace not generated", "HELLO", tf.getText());
50: }
51:
52: private boolean flag = false;
53:
54: public void testEnter() {
55: tf.addActionListener(new ActionListener() {
56: public void actionPerformed(ActionEvent ae) {
57: flag = true;
58: }
59: });
60: showFrame(tf);
61: robot.focus(tf);
62: robot.waitForIdle();
63: robot.keyString("HELLO\n");
64: robot.waitForIdle();
65: assertTrue("'Enter' not generated", flag);
66: }
67:
68: /** Check proper generation of keystrokes given key codes. */
69: public void testKeyStrokeParsing() {
70: assertEquals("Normal 'a'", KeyStroke.getKeyStroke(
71: KeyEvent.VK_A, 0), KeyStrokeMap.getKeyStroke('a'));
72: assertEquals("Shifted 'a'", KeyStroke.getKeyStroke(
73: KeyEvent.VK_A, InputEvent.SHIFT_MASK), KeyStrokeMap
74: .getKeyStroke('A'));
75: }
76:
77: public static void main(String[] args) {
78: TestHelper.runTests(args, KeyStrokeMapTest.class);
79: }
80: }
|