01: package abbot.tester;
02:
03: import java.awt.*;
04: import java.io.File;
05: import javax.swing.*;
06:
07: import junit.extensions.abbot.*;
08:
09: public class JFileChooserTesterTest extends ComponentTestFixture {
10:
11: private JFileChooserTester tester;
12: private JFileChooser chooser;
13: private int result;
14:
15: private static final int ANY = 0;
16: private static final int OPEN = 1;
17: private static final int SAVE = 2;
18:
19: protected void setUp() throws Exception {
20: tester = new JFileChooserTester();
21: chooser = new JFileChooser();
22: show(ANY);
23: }
24:
25: protected Dialog show(final int which) throws Exception {
26: return showModalDialog(new Runnable() {
27: public void run() {
28: switch (which) {
29: case ANY:
30: result = chooser.showDialog(null, "<accept text>");
31: break;
32: case OPEN:
33: result = chooser.showOpenDialog(null);
34: break;
35: case SAVE:
36: result = chooser.showSaveDialog(null);
37: break;
38: }
39: }
40: });
41: }
42:
43: public void testSetFilenameAndApprove() throws Exception {
44: String FILENAME = "goobler";
45: tester.actionSetFilename(chooser, FILENAME);
46: tester.actionApprove(chooser);
47: assertEquals("File should have been approved",
48: JFileChooser.APPROVE_OPTION, result);
49: assertTrue("No file selected",
50: chooser.getSelectedFile() != null);
51: assertEquals("Wrong file selected", FILENAME, chooser
52: .getSelectedFile().getName());
53: }
54:
55: public void testSetDirectory() throws Exception {
56: getRobot().delay(10000);
57: File dir = new File(System.getProperty("user.home"));
58: File pwd = chooser.getCurrentDirectory();
59: // Make sure the directory actually gets changed
60: if (pwd.equals(dir)) {
61: File tmp = File.createTempFile(getName(), ".tmp");
62: tmp.deleteOnExit();
63: dir = tmp.getParentFile();
64: }
65: tester.actionSetDirectory(chooser, dir.getAbsolutePath());
66: assertEquals("Directory not selected", dir, chooser
67: .getCurrentDirectory());
68: }
69:
70: public void testCancel() throws Exception {
71: tester.actionCancel(chooser);
72: assertEquals("File should have been canceled",
73: JFileChooser.CANCEL_OPTION, result);
74: }
75:
76: /** Create a new test case with the given name. */
77: public JFileChooserTesterTest(String name) {
78: super (name);
79: }
80:
81: public static void main(String[] args) {
82: RepeatHelper.runTests(args, JFileChooserTesterTest.class);
83: }
84: }
|