1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package framework;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.Container;
|
---|
6 | import java.awt.KeyboardFocusManager;
|
---|
7 | import java.awt.Point;
|
---|
8 | import java.awt.Window;
|
---|
9 | import java.awt.event.InputEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.MouseEvent;
|
---|
12 |
|
---|
13 | import javax.swing.AbstractButton;
|
---|
14 | import javax.swing.JDialog;
|
---|
15 | import javax.swing.JFrame;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 | import javax.swing.JMenu;
|
---|
18 | import javax.swing.KeyStroke;
|
---|
19 | import javax.swing.SwingUtilities;
|
---|
20 |
|
---|
21 | import junit.extensions.jfcunit.JFCTestCase;
|
---|
22 | import junit.extensions.jfcunit.RobotTestHelper;
|
---|
23 | import junit.extensions.jfcunit.eventdata.DragEventData;
|
---|
24 | import junit.extensions.jfcunit.eventdata.KeyEventData;
|
---|
25 | import junit.extensions.jfcunit.eventdata.MouseEventData;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.data.Preferences;
|
---|
29 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
30 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
31 |
|
---|
32 | abstract 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 | }
|
---|