001: package junit.extensions.abbot;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.util.ArrayList;
006: import java.util.Arrays;
007: import java.util.Iterator;
008: import java.util.List;
009: import javax.swing.*;
010:
011: import abbot.tester.Robot;
012: import abbot.tester.ComponentTester;
013: import abbot.util.*;
014: import abbot.finder.matchers.*;
015: import abbot.Log;
016:
017: /**
018: * Verify {@link ComponentTestFixture} operation.
019: */
020: public class ComponentTestFixtureTest extends ComponentTestFixture {
021:
022: private class MouseWatcher extends MouseAdapter {
023: private boolean mousePressed;
024:
025: public void mousePressed(MouseEvent e) {
026: mousePressed = true;
027: }
028: }
029:
030: private Frame frame;
031: private Component component;
032:
033: /** Used by {@link #testAutoDisposeFields}. */
034: public void testWithShowingFrame() {
035: frame = showFrame(component = new JLabel(getName()));
036: }
037:
038: /** Ensure any fields derived from {@link Component} or
039: {@link ComponentTester} are set to <code>null</code> to allow for
040: proper GC of these objects.
041: */
042: public void testAutoDisposeFields() throws Throwable {
043: ComponentTestFixtureTest test = new ComponentTestFixtureTest();
044: test.setName("testWithShowingFrame");
045: // This invokes setup/run/teardown
046: test.runBare();
047: assertNull("Member Frame should be reset to null after test",
048: test.frame);
049: assertNull(
050: "Member Component should be reset to null after test",
051: test.component);
052: assertNull(
053: "Member ComponentTester should be reset to null after test",
054: test.component);
055: }
056:
057: /** Ensure any {@link Window}s shown during a test are disposed after the
058: test completes.
059: */
060: public void testAutoDisposeWindows() throws Throwable {
061: ComponentTestFixtureTest test = new ComponentTestFixtureTest();
062: test.setName("testWithShowingFrame");
063: List list = Arrays.asList(Frame.getFrames());
064: // This invokes setup/run/teardown
065: test.runBare();
066: List list2 = new ArrayList(Arrays.asList(Frame.getFrames()));
067: list2.removeAll(list);
068: for (Iterator iter = list2.iterator(); iter.hasNext();) {
069: Window w = (Window) iter.next();
070: assertFalse("All test windows should have been disposed", w
071: .isShowing());
072: }
073: }
074:
075: /** Ensure any frame is ready to receive input after showFrame/Window. */
076: public void testShowFrame() throws Throwable {
077: // only required in robot mode
078: if (Robot.getEventMode() != Robot.EM_ROBOT)
079: return;
080:
081: int expected = 10;
082: int count = 0;
083: for (int i = 0; i < expected; i++) {
084: JList list = new JList(new String[] { "one", "two",
085: "three", "four", "five" });
086: MouseWatcher watcher = new MouseWatcher();
087: list.addMouseListener(watcher);
088: Frame f = showFrame(list, new Dimension(200, 200));
089: java.awt.Robot robot = new java.awt.Robot();
090: Point pt = f.getLocationOnScreen();
091: robot.mouseMove(pt.x + f.getWidth() / 2, pt.y
092: + f.getHeight() / 2);
093: robot.mousePress(MouseEvent.BUTTON1_MASK);
094: robot.mouseRelease(MouseEvent.BUTTON1_MASK);
095: robot.delay(Robot.getEventPostDelay());
096: // Use the improved idle wait
097: getRobot().waitForIdle();
098: if (watcher.mousePressed) {
099: ++count;
100: }
101: f.setVisible(false);
102: }
103: assertEquals("Missed some clicks", expected, count);
104: }
105:
106: public void testShowHideShow() {
107: Frame f = showFrame(new JLabel(getName()));
108: assertTrue("Frame should be showing", f.isShowing());
109: if (Robot.getEventMode() == Robot.EM_ROBOT) {
110: assertTrue("Frame should be ready", getWindowTracker()
111: .isWindowReady(f));
112: }
113: hideWindow(f);
114: assertTrue("Frame should be hidden", !f.isShowing());
115: showWindow(f);
116: assertTrue("Frame not showing after hide", f.isShowing());
117: }
118:
119: // FIXME this fails on linux when run with the full suite
120: public void testShowDisposeShow() {
121: Frame f = showFrame(new JLabel(getName()));
122: assertTrue("Frame should be showing", f.isShowing());
123: if (Robot.getEventMode() == Robot.EM_ROBOT) {
124: assertTrue("Frame should be ready", getWindowTracker()
125: .isWindowReady(f));
126: }
127: Log.log("dispose");
128: disposeWindow(f);
129: assertTrue("Frame showing after dispose", !f.isShowing());
130: Log.log("show");
131: try {
132: showWindow(f);
133: Log.log("show done");
134: assertTrue("Frame not showing after a previous dispose", f
135: .isShowing());
136: } finally {
137: // Have to do this manually, since the auto-dispose will
138: // have already ignored this frame since we disposed it
139: f.dispose();
140: }
141: }
142:
143: public void testIsShowingWithFrame() {
144: Frame frame = showFrame(new JLabel(getName()));
145: assertEquals("Frame was shown, but isShowing doesn't agree",
146: true, isShowing(frame.getTitle()));
147: hideWindow(frame);
148: assertEquals("Frame was hidden, but isShowing doesn't agree",
149: false, isShowing(frame.getTitle()));
150: }
151:
152: public void testisShowingWithFrameAndDialog() {
153: final String title = "Not me";
154: final String title2 = "Wait for me";
155: Frame frame = showFrame(new Label(getName()), new Dimension(
156: 200, 200));
157: JDialog d1 = new JDialog(frame, title, true);
158: d1.getContentPane().add(new JLabel("Modal dialog"));
159: JDialog d2 = new JDialog(d1, title2, false);
160: d2.getContentPane().add(new JLabel("Non-modal subdialog"));
161: showWindow(d1);
162: showWindow(d2);
163: assertTrue("Second dialog should have been detected",
164: isShowing(title2));
165: }
166:
167: private void checkDialog(Frame parent, String title) {
168: // Now try it with a dialog
169: JDialog d = new JDialog(parent, title);
170: d.getContentPane().add(new JLabel("I'm a dialog, and I'm OK"));
171:
172: showWindow(d);
173: assertTrue("Dialog should be showing", d.isShowing());
174: assertTrue("Dialog '" + title
175: + "' wasn't showing or wasn't detected",
176: isShowing(title));
177:
178: hideWindow(d);
179: assertFalse("Dialog should not be showing after hideWindow", d
180: .isShowing());
181: assertFalse("Dialog '" + title + "' wasn't hidden",
182: isShowing(title));
183: }
184:
185: public void testIsShowingWithDialog() {
186: Frame frame = showFrame(new JPanel(), new Dimension(200, 200));
187: String title = frame.getTitle();
188: assertTrue("Frame '" + title
189: + "' wasn't showing or wasn't detected",
190: isShowing(title));
191:
192: // Try a dialog with a non-null parent
193: //System.out.println("show/hide w/frame");
194: checkDialog(frame, "Dialog under " + frame.getTitle());
195:
196: // Now try one with a null parent
197: //System.out.println("show/hide w/o frame");
198: checkDialog(null, "Dialog under null frame");
199: Iterator iter = getHierarchy().getRoots().iterator();
200: while (iter.hasNext())
201: getHierarchy().dispose((Window) iter.next());
202:
203: // And make sure we can find an identical one the next time it's shown
204: //System.out.println("show/hide w/o frame, again");
205: checkDialog(null, "Dialog under null frame");
206: }
207:
208: public void testModalDialogWontBlock() throws Exception {
209: JButton b = new JButton("Push me");
210: Frame f = showFrame(b);
211: final JDialog d = new JDialog(f, "Modal Dialog", true);
212: d.getContentPane().add(new JLabel("Modal Dialog Contents"));
213: d.getContentPane().setBackground(Color.red);
214: b.addActionListener(new ActionListener() {
215: public void actionPerformed(ActionEvent e) {
216: d.pack();
217: d.setVisible(true);
218: }
219: });
220: getRobot().click(b);
221: getRobot().waitForIdle();
222: assertTrue("Dialog is showing and test thread not blocked", d
223: .isShowing());
224: JDialog found = (JDialog) getFinder().find(
225: new ClassMatcher(JDialog.class, true));
226: assertEquals("Wrong dialog found", d, found);
227: assertEquals("Wrong-colored dialog is showing", Color.red,
228: found.getContentPane().getBackground());
229: }
230:
231: public static class Fixture extends ComponentTestFixture {
232: private RuntimeException edtException;
233: private RuntimeException mainException;
234:
235: public Fixture(RuntimeException edtException) {
236: this (edtException, null);
237: }
238:
239: public Fixture(RuntimeException edtException,
240: RuntimeException mainException) {
241: super ("testCatchEventThreadException");
242: this .edtException = edtException;
243: this .mainException = mainException;
244: }
245:
246: public void testCatchEventThreadException() throws Throwable {
247: SwingUtilities.invokeLater(new Runnable() {
248: public void run() {
249: throw edtException;
250: }
251: });
252: SwingUtilities.invokeAndWait(new Runnable() {
253: public void run() {
254: }
255: });
256: if (mainException != null)
257: throw mainException;
258: }
259: }
260:
261: public void testFailWithEventThreadException() throws Throwable {
262: class TestException extends RuntimeException {
263: public TestException() {
264: super ("Test exception");
265: }
266: }
267: final TestException e = new TestException();
268: ComponentTestFixture fixture = new Fixture(e);
269: try {
270: fixture.runBare();
271: fail("Test should re-throw the event dispatch exception");
272: } catch (ComponentTestFixture.EventDispatchException thrown) {
273: assertEquals("EDT exception should be thrown", e, thrown
274: .getTargetException());
275: EDTExceptionCatcher.clear();
276: }
277: }
278:
279: public void testFailOnFirstExceptionThrown() throws Throwable {
280: class TestException extends RuntimeException {
281: public TestException() {
282: super ("Test exception");
283: }
284: }
285: final TestException e = new TestException();
286: final TestException main = new TestException();
287: ComponentTestFixture fixture = new Fixture(e, main);
288: try {
289: fixture.runBare();
290: fail("Test should re-throw the event dispatch exception");
291: } catch (ComponentTestFixture.EventDispatchException thrown) {
292: assertEquals("Exception on EDT should be thrown", e, thrown
293: .getTargetException());
294: EDTExceptionCatcher.clear();
295: }
296: }
297:
298: public static void main(String[] args) {
299: RepeatHelper.runTests(args, ComponentTestFixtureTest.class);
300: }
301: }
|