source: josm/trunk/test/functional/framework/FunctionalTestCase.java@ 321

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