001: package abbot.tester;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import javax.swing.*;
006: import junit.framework.*;
007: import junit.extensions.abbot.*;
008: import junit.extensions.abbot.Timer;
009:
010: public class WindowTrackerTest extends ComponentTestFixture {
011:
012: private class MouseWatcher extends MouseAdapter {
013: private boolean gotClick = false;
014:
015: public void mousePressed(MouseEvent e) {
016: gotClick = true;
017: }
018: }
019:
020: private WindowTracker tracker;
021:
022: private void wait(Window w, boolean state, String msg) {
023: Timer timer = new Timer();
024: while (tracker.isWindowReady(w) != state) {
025: if (timer.elapsed() > WindowTracker.WINDOW_READY_DELAY + 500)
026: fail(msg);
027: try {
028: Thread.sleep(10);
029: } catch (InterruptedException e) {
030: }
031: }
032: }
033:
034: protected void setUp() {
035: tracker = new WindowTracker();
036: }
037:
038: protected void tearDown() {
039: tracker = null;
040: }
041:
042: private class MissedShow1 extends AssertionFailedError {
043: public MissedShow1() {
044: super ("Window not ready after initial show");
045: }
046: }
047:
048: private class MissedShow2 extends AssertionFailedError {
049: public MissedShow2() {
050: super ("Window not ready after subsequent show");
051: }
052: }
053:
054: public void testTrackWindowState() throws Throwable {
055: Frame w = new Frame(getName());
056: w.pack();
057: w.setVisible(true);
058: wait(w, true,
059: "WindowTracker didn't catch initial Window.show()");
060: assertTrue("Window should be showing", w.isShowing());
061:
062: w.setVisible(false);
063: wait(w, false, "WindowTracker didn't catch Window.hide()");
064: assertTrue("Window should not be showing", !w.isShowing());
065:
066: w.setVisible(true);
067: wait(w, true,
068: "WindowTracker didn't catch Window.show() after hide");
069: assertTrue("Window should be showing", w.isShowing());
070:
071: // Generates WINDOW_CLOSE, may or may not generate COMPONENT_HIDDEN
072: w.dispose();
073: wait(w, false, "WindowTracker didn't catch Window.dispose()");
074: assertTrue("Window should not be showing", !w.isShowing());
075: }
076:
077: public void testSingleWindowReady() throws Throwable {
078: JFrame f = new JFrame(getName());
079: MouseWatcher watcher = new MouseWatcher();
080: f.addMouseListener(watcher);
081: f.getContentPane().add(new JLabel(getName()));
082: f.pack();
083: f.setSize(100, 100);
084: f.setVisible(true);
085: try {
086: wait(f, true,
087: "WindowTracker didn't catch initial Window.show");
088: getRobot().click(f);
089: getRobot().waitForIdle();
090: if (!watcher.gotClick)
091: throw new MissedShow1();
092:
093: watcher.gotClick = false;
094: f.setVisible(false);
095: wait(f, false,
096: "WindowTracker didn't catch Window.hide after show");
097:
098: f.setVisible(true);
099: wait(f, true,
100: "WindowTracker didn't catch Window.show after hide");
101: assertTrue("Window not ready", tracker.isWindowReady(f));
102: getRobot().click(f);
103: getRobot().waitForIdle();
104: if (!watcher.gotClick)
105: throw new MissedShow2();
106: } finally {
107: f.dispose();
108: }
109: }
110:
111: public void testComplexWindowReady() throws Throwable {
112: JList list = new JList(new String[] { "one", "two", "three",
113: "four", "five" });
114: JFrame f = new JFrame(getName() + "1");
115: f.getContentPane().add(list);
116: MouseWatcher watcher = new MouseWatcher();
117: list.addMouseListener(watcher);
118: f.pack();
119: f.setLocation(100, 100);
120: f.setVisible(true);
121: try {
122: wait(f, true, "WindowTracker didn't catch window");
123:
124: getRobot().click(list);
125: getRobot().waitForIdle();
126:
127: assertTrue("Window not ready for click", watcher.gotClick);
128: } finally {
129: f.dispose();
130: }
131: }
132:
133: // Not required when using mouse motion to detect window readiness
134: /*
135: public void testTrackRepeatedWindowReady() throws Throwable {
136: // This test is only required in Robot mode
137: if (Robot.getEventMode() != Robot.EM_ROBOT)
138: return;
139: int EXPECTED = 100;
140: int count1 = EXPECTED;
141: int count2 = EXPECTED;
142: int count3 = EXPECTED;
143: for (int i=0;i < EXPECTED;i++) {
144: try {
145: testSingleWindowReady();
146: }
147: catch(MissedShow1 e) {
148: --count1;
149: }
150: catch(MissedShow2 e) {
151: --count2;
152: }
153: try {
154: testComplexWindowReady();
155: }
156: catch(AssertionFailedError e) {
157: --count3;
158: }
159: }
160: assertEquals("Missed some clicks on initial show", EXPECTED, count1);
161: assertEquals("Missed some clicks on subsequent show", EXPECTED, count2);
162: assertEquals("Missed clicks on complex window", EXPECTED, count3);
163: }
164: */
165: public void testTrackWindowReadyAfterDispose() throws Exception {
166: final Frame w = new Frame(getName());
167: getRobot().invokeAndWait(new Runnable() {
168: public void run() {
169: w.pack();
170: w.setVisible(true);
171: }
172: });
173: wait(w, true, "Didn't catch initial Window.show()");
174: assertTrue("Window not showing", w.isShowing());
175:
176: getRobot().invokeAndWait(new Runnable() {
177: public void run() {
178: w.dispose();
179: }
180: });
181: wait(w, false, "Didn't catch Window.dispose()");
182: assertTrue("Window not hidden", !w.isShowing());
183:
184: getRobot().invokeAndWait(new Runnable() {
185: public void run() {
186: w.pack();
187: w.setVisible(true);
188: }
189: });
190: wait(w, true,
191: "Didn't catch Window.show() after dispose(), pack()");
192: assertTrue("Window not showing", w.isShowing());
193: }
194:
195: public void testDetectEmptyDialog() {
196: final Frame w = new Frame(getName());
197: final Dialog d = new Dialog(w, getName());
198: getRobot().invokeAndWait(new Runnable() {
199: public void run() {
200: w.pack();
201: w.setVisible(true);
202: d.pack();
203: d.setVisible(true);
204: }
205: });
206: wait(d, true, "Didn't catch empty dialog");
207: assertTrue("Dialog not showing", d.isShowing());
208: }
209:
210: public static void main(String[] args) {
211: RepeatHelper.runTests(args, WindowTrackerTest.class);
212: }
213: }
|