1
|
|
package junit.extensions.abbot;
|
2
|
|
|
3
|
|
import java.awt.*;
|
4
|
|
import java.awt.event.*;
|
5
|
|
import java.util.ArrayList;
|
6
|
|
import java.util.Arrays;
|
7
|
|
import java.util.Iterator;
|
8
|
|
import java.util.List;
|
9
|
|
import javax.swing.*;
|
10
|
|
|
11
|
|
import abbot.script.*;
|
12
|
|
import abbot.tester.Robot;
|
13
|
|
import abbot.tester.ComponentTester;
|
14
|
|
import abbot.util.*;
|
15
|
|
import abbot.util.AWTFixtureHelper;
|
16
|
|
import abbot.finder.matchers.*;
|
17
|
|
import abbot.Log;
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
public class ComponentTestFixtureTest extends ComponentTestFixture {
|
23
|
|
|
24
|
|
private class MouseWatcher extends MouseAdapter {
|
25
|
|
private boolean mousePressed;
|
26
|
19
|
public void mousePressed(MouseEvent e) {
|
27
|
19
|
mousePressed = true;
|
28
|
|
}
|
29
|
|
}
|
30
|
|
|
31
|
|
private Frame frame;
|
32
|
|
private Component component;
|
33
|
|
|
34
|
|
|
35
|
3
|
public void testWithShowingFrame() {
|
36
|
3
|
frame = showFrame(component = new JLabel(getName()));
|
37
|
|
}
|
38
|
|
|
39
|
|
|
40
|
|
|
41
|
|
|
42
|
|
|
43
|
1
|
public void testAutoDisposeFields() throws Throwable {
|
44
|
1
|
ComponentTestFixtureTest test = new ComponentTestFixtureTest();
|
45
|
1
|
test.setName("testWithShowingFrame");
|
46
|
|
|
47
|
1
|
test.runBare();
|
48
|
1
|
assertNull("Member Frame should be reset to null after test",
|
49
|
|
test.frame);
|
50
|
1
|
assertNull("Member Component should be reset to null after test",
|
51
|
|
test.component);
|
52
|
1
|
assertNull("Member ComponentTester should be reset to null after test",
|
53
|
|
test.component);
|
54
|
|
}
|
55
|
|
|
56
|
|
|
57
|
|
|
58
|
|
|
59
|
1
|
public void testAutoDisposeWindows() throws Throwable {
|
60
|
1
|
ComponentTestFixtureTest test = new ComponentTestFixtureTest();
|
61
|
1
|
test.setName("testWithShowingFrame");
|
62
|
1
|
List list = Arrays.asList(Frame.getFrames());
|
63
|
|
|
64
|
1
|
test.runBare();
|
65
|
1
|
List list2 = new ArrayList(Arrays.asList(Frame.getFrames()));
|
66
|
1
|
list2.removeAll(list);
|
67
|
1
|
for (Iterator iter=list2.iterator();iter.hasNext();) {
|
68
|
1
|
Window w = (Window)iter.next();
|
69
|
1
|
assertFalse("All test windows should have been disposed",
|
70
|
|
w.isShowing());
|
71
|
|
}
|
72
|
|
}
|
73
|
|
|
74
|
|
|
75
|
1
|
public void testShowFrame() throws Throwable {
|
76
|
|
|
77
|
1
|
if (getRobot().getEventMode() != Robot.EM_ROBOT)
|
78
|
0
|
return;
|
79
|
|
|
80
|
1
|
int expected = 10;
|
81
|
1
|
int count = 0;
|
82
|
1
|
for (int i=0;i < expected;i++) {
|
83
|
10
|
JList list = new JList(new String[] {
|
84
|
|
"one", "two", "three", "four", "five"
|
85
|
|
});
|
86
|
10
|
MouseWatcher watcher = new MouseWatcher();
|
87
|
10
|
list.addMouseListener(watcher);
|
88
|
10
|
Frame f = showFrame(list, new Dimension(200, 200));
|
89
|
10
|
java.awt.Robot robot = new java.awt.Robot();
|
90
|
10
|
Point pt = f.getLocationOnScreen();
|
91
|
10
|
robot.mouseMove(pt.x + f.getWidth()/2,
|
92
|
|
pt.y + f.getHeight()/2);
|
93
|
10
|
robot.mousePress(MouseEvent.BUTTON1_MASK);
|
94
|
10
|
robot.mouseRelease(MouseEvent.BUTTON1_MASK);
|
95
|
10
|
robot.delay(Robot.getEventPostDelay());
|
96
|
|
|
97
|
10
|
getRobot().waitForIdle();
|
98
|
10
|
if (watcher.mousePressed) {
|
99
|
9
|
++count;
|
100
|
|
}
|
101
|
10
|
f.setVisible(false);
|
102
|
|
}
|
103
|
1
|
assertEquals("Missed some clicks", expected, count);
|
104
|
|
}
|
105
|
|
|
106
|
|
|
107
|
1
|
public void testShowWindow() throws Exception {
|
108
|
|
|
109
|
1
|
if (getRobot().getEventMode() != Robot.EM_ROBOT)
|
110
|
0
|
return;
|
111
|
1
|
java.awt.Robot robot = new java.awt.Robot();
|
112
|
|
|
113
|
1
|
int expected = 10;
|
114
|
1
|
int count = 0;
|
115
|
1
|
for (int i=0;i < expected;i++) {
|
116
|
10
|
JList list = new JList(new String[] {
|
117
|
|
"one", "two", "three", "four", "five"
|
118
|
|
});
|
119
|
10
|
JFrame f = new JFrame(getName());
|
120
|
10
|
f.getContentPane().add(list);
|
121
|
10
|
MouseWatcher watcher = new MouseWatcher();
|
122
|
10
|
list.addMouseListener(watcher);
|
123
|
10
|
showWindow(f);
|
124
|
|
|
125
|
10
|
Point pt = list.getLocationOnScreen();
|
126
|
10
|
robot.mouseMove(pt.x + list.getWidth()/2,
|
127
|
|
pt.y + list.getHeight()/2);
|
128
|
10
|
robot.mousePress(MouseEvent.BUTTON1_MASK);
|
129
|
10
|
robot.mouseRelease(MouseEvent.BUTTON1_MASK);
|
130
|
10
|
robot.delay(Robot.getEventPostDelay());
|
131
|
|
|
132
|
10
|
getRobot().waitForIdle();
|
133
|
10
|
if (watcher.mousePressed) {
|
134
|
10
|
Log.log("pressed");
|
135
|
10
|
++count;
|
136
|
|
}
|
137
|
|
else {
|
138
|
0
|
Log.log("missed");
|
139
|
|
}
|
140
|
10
|
f.setVisible(false);
|
141
|
|
}
|
142
|
1
|
assertEquals("Missed some clicks", expected, count);
|
143
|
|
}
|
144
|
|
|
145
|
|
|
146
|
1
|
public void testShowHideShow() {
|
147
|
1
|
Frame f = showFrame(new JLabel(getName()));
|
148
|
1
|
assertTrue("Frame should be showing", f.isShowing());
|
149
|
1
|
assertTrue("Frame should be ready",
|
150
|
|
getWindowTracker().isWindowReady(f));
|
151
|
1
|
hideWindow(f);
|
152
|
1
|
assertTrue("Frame should be hidden" , !f.isShowing());
|
153
|
1
|
showWindow(f);
|
154
|
1
|
assertTrue("Frame not showing after hide", f.isShowing());
|
155
|
|
}
|
156
|
|
|
157
|
|
|
158
|
|
|
159
|
1
|
public void testShowDisposeShow() {
|
160
|
1
|
Frame f = showFrame(new JLabel(getName()));
|
161
|
1
|
assertTrue("Frame should be showing", f.isShowing());
|
162
|
1
|
assertTrue("Frame should be ready",
|
163
|
|
getWindowTracker().isWindowReady(f));
|
164
|
1
|
Log.log("dispose");
|
165
|
1
|
disposeWindow(f);
|
166
|
1
|
assertTrue("Frame showing after dispose", !f.isShowing());
|
167
|
1
|
Log.log("show");
|
168
|
1
|
showWindow(f);
|
169
|
1
|
Log.log("show done");
|
170
|
1
|
assertTrue("Frame not showing after a previous dispose",
|
171
|
|
f.isShowing());
|
172
|
|
}
|
173
|
|
|
174
|
1
|
public void testIsShowing() {
|
175
|
1
|
Frame frame = showFrame(new JLabel(getName()));
|
176
|
1
|
assertEquals("Frame was shown, but isShowing doesn't agree",
|
177
|
|
true, isShowing(frame.getTitle()));
|
178
|
1
|
hideWindow(frame);
|
179
|
1
|
assertEquals("Frame was hidden, but isShowing doesn't agree",
|
180
|
|
false, isShowing(frame.getTitle()));
|
181
|
|
}
|
182
|
|
|
183
|
1
|
public void testFrameShowingAssertionWithDialog() {
|
184
|
1
|
final String title = "Not me";
|
185
|
1
|
final String title2 = "Wait for me";
|
186
|
1
|
Frame frame = showFrame(new Label(getName()), new Dimension(200, 200));
|
187
|
1
|
JDialog d1 = new JDialog(frame, title, true);
|
188
|
1
|
d1.getContentPane().add(new JLabel("Modal dialog"));
|
189
|
1
|
JDialog d2 = new JDialog(d1, title2, false);
|
190
|
1
|
d2.getContentPane().add(new JLabel("Non-modal subdialog"));
|
191
|
1
|
showWindow(d1);
|
192
|
1
|
showWindow(d2);
|
193
|
1
|
assertTrue("Second dialog should have been detected",
|
194
|
|
isShowing(title2));
|
195
|
|
}
|
196
|
|
|
197
|
3
|
private void checkDialog(Frame parent, String title) {
|
198
|
|
|
199
|
3
|
JDialog d = new JDialog(parent, title);
|
200
|
3
|
d.getContentPane().add(new JLabel("I'm a dialog, and I'm OK"));
|
201
|
|
|
202
|
3
|
showWindow(d);
|
203
|
3
|
assertTrue("Dialog '" + title + "' wasn't showing or wasn't detected",
|
204
|
|
isShowing(title));
|
205
|
3
|
hideWindow(d);
|
206
|
3
|
assertTrue("Dialog '" + title + "' wasn't hidden", !isShowing(title));
|
207
|
|
}
|
208
|
|
|
209
|
1
|
public void testIsShowingDialog() {
|
210
|
1
|
Frame frame = showFrame(new JPanel(), new Dimension(200, 200));
|
211
|
1
|
String title = frame.getTitle();
|
212
|
1
|
assertTrue("Frame '" + title + "' wasn't showing or wasn't detected",
|
213
|
|
isShowing(title));
|
214
|
|
|
215
|
|
|
216
|
1
|
checkDialog(frame, "Dialog under " + frame.getTitle());
|
217
|
|
|
218
|
1
|
checkDialog(null, "Dialog under null frame");
|
219
|
1
|
Iterator iter = getHierarchy().getRoots().iterator();
|
220
|
1
|
while (iter.hasNext())
|
221
|
2
|
getHierarchy().dispose((Window)iter.next());
|
222
|
|
|
223
|
1
|
checkDialog(null, "Dialog under null frame");
|
224
|
|
}
|
225
|
|
|
226
|
1
|
public void testModalDialogWontBlock() throws Exception {
|
227
|
1
|
JButton b = new JButton("Push me");
|
228
|
1
|
Frame f = showFrame(b);
|
229
|
1
|
final JDialog d = new JDialog(f, "Modal Dialog", true);
|
230
|
1
|
d.getContentPane().add(new JLabel("Modal Dialog Contents"));
|
231
|
1
|
d.getContentPane().setBackground(Color.red);
|
232
|
1
|
b.addActionListener(new ActionListener() {
|
233
|
1
|
public void actionPerformed(ActionEvent e) {
|
234
|
1
|
d.pack();
|
235
|
1
|
d.show();
|
236
|
|
}
|
237
|
|
});
|
238
|
1
|
getRobot().click(b);
|
239
|
1
|
getRobot().waitForIdle();
|
240
|
1
|
assertTrue("Dialog is showing and test thread not blocked",
|
241
|
|
d.isShowing());
|
242
|
1
|
JDialog found = (JDialog)
|
243
|
|
getFinder().find(new ClassMatcher(JDialog.class, true));
|
244
|
1
|
assertEquals("Wrong dialog found", d, found);
|
245
|
1
|
assertEquals("Wrong-colored dialog is showing",
|
246
|
|
Color.red, found.getContentPane().getBackground());
|
247
|
|
}
|
248
|
|
|
249
|
|
public static class Fixture extends ComponentTestFixture {
|
250
|
|
private RuntimeException edtException;
|
251
|
|
private RuntimeException mainException;
|
252
|
1
|
public Fixture(RuntimeException edtException) {
|
253
|
1
|
this(edtException, null);
|
254
|
|
}
|
255
|
2
|
public Fixture(RuntimeException edtException,
|
256
|
|
RuntimeException mainException) {
|
257
|
2
|
super("testCatchEventThreadException");
|
258
|
2
|
this.edtException = edtException;
|
259
|
2
|
this.mainException = mainException;
|
260
|
|
}
|
261
|
2
|
public void testCatchEventThreadException() throws Throwable {
|
262
|
2
|
SwingUtilities.invokeLater(new Runnable() {
|
263
|
2
|
public void run() {
|
264
|
2
|
throw edtException;
|
265
|
|
}
|
266
|
|
});
|
267
|
2
|
SwingUtilities.invokeAndWait(new Runnable() {
|
268
|
2
|
public void run() { }
|
269
|
|
});
|
270
|
2
|
if (mainException != null)
|
271
|
1
|
throw mainException;
|
272
|
|
}
|
273
|
|
}
|
274
|
|
|
275
|
1
|
public void testFailWithEventThreadException() throws Throwable {
|
276
|
|
class TestException extends RuntimeException {
|
277
|
1
|
public TestException() { super("Test exception"); }
|
278
|
|
}
|
279
|
1
|
final TestException e = new TestException();
|
280
|
1
|
ComponentTestFixture fixture = new Fixture(e);
|
281
|
1
|
try {
|
282
|
1
|
fixture.runBare();
|
283
|
0
|
fail("Test should re-throw the event dispatch exception");
|
284
|
|
}
|
285
|
|
catch(RuntimeException thrown) {
|
286
|
1
|
assertEquals("EDT exception should be thrown", e, thrown);
|
287
|
1
|
EDTExceptionCatcher.clear();
|
288
|
|
}
|
289
|
|
}
|
290
|
|
|
291
|
1
|
public void testFailOnFirstExceptionThrown() throws Throwable {
|
292
|
|
class TestException extends RuntimeException {
|
293
|
2
|
public TestException() { super("Test exception"); }
|
294
|
|
}
|
295
|
1
|
final TestException e = new TestException();
|
296
|
1
|
final TestException main = new TestException();
|
297
|
1
|
ComponentTestFixture fixture = new Fixture(e, main);
|
298
|
1
|
try {
|
299
|
1
|
fixture.runBare();
|
300
|
0
|
fail("Test should re-throw the event dispatch exception");
|
301
|
|
}
|
302
|
|
catch(RuntimeException thrown) {
|
303
|
1
|
assertEquals("Exception on EDT should be thrown", e, thrown);
|
304
|
1
|
EDTExceptionCatcher.clear();
|
305
|
|
}
|
306
|
|
}
|
307
|
|
|
308
|
0
|
public static void main(String[] args) {
|
309
|
0
|
RepeatHelper.runTests(args, ComponentTestFixtureTest.class);
|
310
|
|
}
|
311
|
|
}
|
312
|
|
|