001: package abbot.tester;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import java.io.*;
006:
007: import junit.extensions.abbot.*;
008: import abbot.*;
009: import abbot.util.Bugs;
010:
011: /** Unit test to verify the FileDialogTester class.<p> */
012:
013: public class FileDialogTesterTest extends ComponentTestFixture {
014:
015: private File tmpFile;
016: private FileDialogTester tester;
017: private FileDialog dialog;
018:
019: protected void setUp() throws IOException {
020: tester = new FileDialogTester();
021: Frame frame = new Frame(getName() + " Frame");
022: if (Bugs.fileDialogRequiresVisibleFrame()) {
023: showWindow(frame);
024: }
025: dialog = new FileDialog(frame, getName(), FileDialog.LOAD);
026: tmpFile = File.createTempFile(getName(), ".test");
027: tmpFile.deleteOnExit();
028: }
029:
030: protected void tearDown() {
031: if (dialog.isShowing() && Bugs.fileDialogRequiresDismiss()) {
032: tester.actionKeyStroke(KeyEvent.VK_ESCAPE);
033: }
034: dialog = null;
035: tmpFile.delete();
036: tmpFile = null;
037: }
038:
039: public void testSetDirectory() {
040: showWindow(dialog);
041: String dir = tmpFile.getParentFile().getPath();
042: tester.actionSetDirectory(dialog, dir);
043: String ddir = dialog.getDirectory();
044: if (ddir.endsWith(File.separator))
045: ddir = ddir.substring(0, ddir.length() - 1);
046: if (dir.endsWith(File.separator))
047: dir = dir.substring(0, dir.length() - 1);
048: assertEquals("Directory not set", dir, ddir);
049: }
050:
051: public void testSetFileAndAccept() {
052: showWindow(dialog);
053: tester.actionSetFile(dialog, tmpFile.getPath());
054: assertEquals("File not set", tmpFile.getPath(), dialog
055: .getFile());
056: tester.actionAccept(dialog);
057: Timer timer = new Timer();
058: while (dialog.isShowing()) {
059: if (timer.elapsed() > EVENT_GENERATION_DELAY)
060: fail("Dialog should have closed on accept");
061: tester.sleep();
062: }
063: assertEquals("File not set", tmpFile.getPath(), dialog
064: .getFile());
065: assertTrue("Dialog should have been hidden", !dialog
066: .isShowing());
067: }
068:
069: public void testCancel() {
070: showWindow(dialog);
071: tester.actionSetFile(dialog, tmpFile.getPath());
072: tester.actionCancel(dialog);
073: Timer timer = new Timer();
074: while (dialog.isShowing()) {
075: if (timer.elapsed() > EVENT_GENERATION_DELAY)
076: fail("Dialog should have closed on cancel");
077: tester.sleep();
078: }
079: assertEquals("File should not be set", null, dialog.getFile());
080: }
081:
082: /** Simulate actual code that shows a dialog and checks the results. */
083: private class ShowDialog implements Runnable {
084: private boolean onDispatch = false;
085: public volatile String fileResult = null;
086:
087: public ShowDialog(boolean onDispatch) {
088: this .onDispatch = onDispatch;
089: }
090:
091: private void doShow() {
092: FileDialog d = dialog;
093: d.pack();
094: d.setVisible(true);
095: fileResult = d.getFile();
096: }
097:
098: public void run() {
099: if (onDispatch) {
100: tester.invokeLater(new Runnable() {
101: public void run() {
102: doShow();
103: }
104: });
105: } else {
106: doShow();
107: }
108: }
109: }
110:
111: /** Ensure a dialog shown on the event dispatch
112: thread gets the proper result from getFile after FileDialog.show
113: returns. */
114: public void testThreadAcceptDispatch() throws Throwable {
115: ShowDialog sd1 = new ShowDialog(true);
116: Thread t1 = new Thread(sd1, "event dispatch");
117: t1.start();
118: waitForWindow(dialog, true);
119: tester.actionSetFile(dialog, tmpFile.getPath());
120: tester.actionAccept(dialog);
121: t1.join();
122: assertEquals(
123: "File should be set after show on dispatch thread",
124: tmpFile.getPath(), sd1.fileResult);
125: assertTrue("Dialog should have been hidden", !dialog
126: .isShowing());
127: }
128:
129: /** Ensure a dialog shown off the event dispatch
130: thread gets the proper result from getFile after FileDialog.show
131: returns. */
132: public void testThreadAcceptNonDispatch() throws Throwable {
133: ShowDialog sd2 = new ShowDialog(false);
134: Thread t2 = new Thread(sd2, "non-event dispatch");
135: t2.start();
136: waitForWindow(dialog, true);
137: tester.actionSetFile(dialog, tmpFile.getPath());
138: tester.actionAccept(dialog);
139: t2.join();
140: // NOTE: this may fail due to race conditions
141: assertEquals(
142: "File should be set after show from non-dispatch thread",
143: tmpFile.getPath(), sd2.fileResult);
144: assertTrue("Dialog should have been hidden", !dialog
145: .isShowing());
146: }
147:
148: public void testThreadCancelDispatch() throws Throwable {
149: ShowDialog sd1 = new ShowDialog(true);
150: Thread t1 = new Thread(sd1, "event dispatch");
151: t1.start();
152: waitForWindow(dialog, true);
153: tester.actionSetFile(dialog, tmpFile.getPath());
154: tester.actionCancel(dialog);
155: t1.join();
156: assertNull(
157: "File should not be set after show on dispatch thread",
158: sd1.fileResult);
159: // More OSX bugs?
160: if (Platform.isOSX())
161: tester.delay(2000);
162: assertTrue("Dialog should have been hidden", !dialog
163: .isShowing());
164: }
165:
166: public void testThreadCancelNonDispatch() throws Throwable {
167: ShowDialog sd2 = new ShowDialog(false);
168: Thread t2 = new Thread(sd2, "non-event dispatch");
169: t2.start();
170: waitForWindow(dialog, true);
171: tester.actionSetFile(dialog, tmpFile.getPath());
172: tester.actionCancel(dialog);
173: t2.join();
174: assertNull(
175: "File should not be set after show from non-dispatch thread",
176: sd2.fileResult);
177: assertTrue("Dialog should have been hidden", !dialog
178: .isShowing());
179: }
180:
181: public static void main(String[] args) {
182: RepeatHelper.runTests(args, FileDialogTesterTest.class);
183: }
184: }
|