source: josm/test/functional/framework/FunctionalTestCase.java@ 285

Last change on this file since 285 was 285, checked in by imi, 17 years ago
  • fixed test dependencies
  • added more functional tests
File size: 4.7 KB
Line 
1package framework;
2
3import java.awt.Component;
4import java.awt.Container;
5import java.awt.KeyboardFocusManager;
6import java.awt.Point;
7import java.awt.Window;
8import java.awt.event.InputEvent;
9import java.awt.event.KeyEvent;
10import java.awt.event.MouseEvent;
11
12import javax.swing.AbstractButton;
13import javax.swing.JDialog;
14import javax.swing.JFrame;
15import javax.swing.JLabel;
16import javax.swing.JMenu;
17import javax.swing.KeyStroke;
18import javax.swing.SwingUtilities;
19
20import junit.extensions.jfcunit.JFCTestCase;
21import junit.extensions.jfcunit.RobotTestHelper;
22import junit.extensions.jfcunit.eventdata.DragEventData;
23import junit.extensions.jfcunit.eventdata.KeyEventData;
24import junit.extensions.jfcunit.eventdata.MouseEventData;
25
26import org.openstreetmap.josm.Main;
27import org.openstreetmap.josm.data.Preferences;
28import org.openstreetmap.josm.data.osm.DataSet;
29import org.openstreetmap.josm.gui.MainApplication;
30
31abstract public class FunctionalTestCase extends JFCTestCase {
32
33 private KeyStroke getKey(String s) {
34 int key = 0;
35 int modifier = 0;
36 s = s.toUpperCase();
37 if (s.startsWith("CTRL")) {
38 modifier |= InputEvent.CTRL_MASK;
39 s = s.substring(4);
40 }
41 if (s.startsWith("-"))
42 s = s.substring(1);
43 if (s.startsWith("SHIFT")) {
44 modifier |= InputEvent.SHIFT_MASK;
45 s = s.substring(5);
46 }
47 if (s.startsWith("-"))
48 s = s.substring(1);
49 if (s.startsWith("ALT")) {
50 modifier |= InputEvent.ALT_MASK;
51 s = s.substring(3);
52 }
53 if (s.startsWith("-"))
54 s = s.substring(1);
55 if (s.matches("^F[1-9][012]?$"))
56 key = KeyEvent.VK_F1 + Integer.parseInt(s.substring(1)) - 1;
57 else if (s.length() == 0)
58 key = 0;
59 else if (s.length() != 1)
60 throw new RuntimeException("Illegal key description '"+s+"'.");
61 else
62 key = s.charAt(0);
63 return KeyStroke.getKeyStroke(key, modifier);
64 }
65
66 @Override protected void setUp() throws Exception {
67 super.setUp();
68 setHelper(new RobotTestHelper());
69
70 Main.ds = new DataSet();
71 Main.pref = new Preferences();
72 if (Main.map != null)
73 Main.main.setMapFrame(null);
74
75 MainApplication.main(new String[]{});
76 }
77
78 @Override protected void tearDown() throws Exception {
79 Main.parent.setVisible(false);
80 super.tearDown();
81 }
82
83 protected Component find(Component c, String search) {
84 if (c == null)
85 return null;
86 if (search.equals(c.getName()))
87 return c;
88 if (c instanceof JLabel && search.equals(((JLabel)c).getText()))
89 return c;
90 if (c instanceof AbstractButton && search.equals(((AbstractButton)c).getText()))
91 return c;
92
93 if (c instanceof Container) {
94 Container ct = (Container)c;
95 for (int i = 0; i < ct.getComponentCount(); ++i) {
96 Component result = find(ct.getComponent(i), search);
97 if (result != null)
98 return result;
99 }
100 }
101 if (c instanceof JMenu) {
102 JMenu menu = (JMenu)c;
103 for (int i = 0; i < menu.getMenuComponentCount(); ++i) {
104 Component result = find(menu.getMenuComponent(i), search);
105 if (result != null)
106 return result;
107 }
108 }
109 if (c instanceof JFrame) {
110 Component result = find(((JFrame)c).getJMenuBar(), search);
111 if (result != null)
112 return result;
113 }
114 return null;
115 }
116
117 protected Component find(String s) {
118 Container frame = SwingUtilities.getAncestorOfClass(Window.class, KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
119 return find(frame, s);
120 }
121
122 protected void key(String... keys) {
123 for (String s : keys) {
124 KeyStroke k = getKey(s);
125 getHelper().sendKeyAction(new KeyEventData(this, Main.contentPane, k.getKeyCode(), k.getModifiers(), 0));
126 }
127 }
128
129 protected void key(int... keys) {
130 for (int i : keys) {
131 getHelper().sendKeyAction(new KeyEventData(this, Main.contentPane, i, 0, 0));
132 }
133 }
134
135 /**
136 * Clicks on a spot on the main map (should be open by now)
137 */
138 protected void click(int x, int y) {
139 getHelper().enterClickAndLeave(new MouseEventData(this, Main.map, 1, MouseEvent.BUTTON1_MASK, false, 0, new Point(x,y)));
140 }
141
142 protected void click(int x, int y, String modifier) {
143 getHelper().enterClickAndLeave(new MouseEventData(this, Main.map, 1, MouseEvent.BUTTON1_MASK + getKey(modifier).getModifiers(), false, 0, new Point(x,y)));
144 }
145
146
147 protected void drag(int xfrom, int yfrom, int xto, int yto) {
148 getHelper().enterDragAndLeave(new DragEventData(
149 this,
150 new MouseEventData(this, Main.map, 1, MouseEvent.BUTTON1_MASK, false, 0, new Point(xfrom, yfrom)),
151 new MouseEventData(this, Main.map, 1, MouseEvent.BUTTON1_MASK, false, 0, new Point(xto, yto))));
152 }
153
154
155 protected void assertPopup() {
156 Component focus = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
157 Container dlg = SwingUtilities.getAncestorOfClass(JDialog.class, focus);
158 assertNotNull("Popup dialog found", dlg);
159 key(KeyEvent.VK_ENTER);
160 }
161}
Note: See TracBrowser for help on using the repository browser.