001: package org.uispec4j.interception;
002:
003: import org.uispec4j.Trigger;
004: import org.uispec4j.utils.ArrayUtils;
005: import org.uispec4j.utils.Utils;
006:
007: import javax.swing.*;
008: import java.awt.*;
009: import java.io.File;
010:
011: public class FileChooserHandlerTest extends InterceptionTestCase {
012: private JFileChooser chooser = new JFileChooser();
013: private Trigger SHOW_OPEN_DIALOG_TRIGGER = new Trigger() {
014: public void run() throws Exception {
015: JFrame frame = new JFrame();
016: chooser.showOpenDialog(frame);
017: }
018: };
019: private Trigger SHOW_SAVE_DIALOG_TRIGGER = new Trigger() {
020: public void run() throws Exception {
021: JFrame frame = new JFrame();
022: chooser.showSaveDialog(frame);
023: }
024: };
025: private Trigger SHOW_CUSTOM_DIALOG_TRIGGER = new Trigger() {
026: public void run() throws Exception {
027: JFrame frame = new JFrame();
028: chooser.showDialog(frame, "OK");
029: }
030: };
031: private File javaHome = new File(System.getProperty("java.home"));
032: private File userHome = new File(System.getProperty("user.home"));
033:
034: protected void setUp() throws Exception {
035: super .setUp();
036: chooser
037: .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
038: }
039:
040: public void testSelectionOfASingleFile() throws Exception {
041: WindowInterceptor.init(SHOW_OPEN_DIALOG_TRIGGER).process(
042: FileChooserHandler.init().select(javaHome)).run();
043: assertEquals(javaHome, chooser.getSelectedFile());
044: }
045:
046: public void testSelectionOfSeveralFiles() throws Exception {
047: File[] files = { javaHome, userHome };
048: WindowInterceptor.init(SHOW_OPEN_DIALOG_TRIGGER).process(
049: FileChooserHandler.init().select(files)).run();
050: ArrayUtils.assertEquals(files, chooser.getSelectedFiles());
051: }
052:
053: public void testSelectionOfASingleStringifiedFile()
054: throws Exception {
055: WindowInterceptor.init(SHOW_OPEN_DIALOG_TRIGGER).process(
056: FileChooserHandler.init().select(
057: javaHome.getAbsolutePath())).run();
058: assertEquals(javaHome, chooser.getSelectedFile());
059: }
060:
061: public void testSelectionOfSeveralStringifiedFile()
062: throws Exception {
063: String[] files = { javaHome.getAbsolutePath(),
064: userHome.getAbsolutePath() };
065: WindowInterceptor.init(SHOW_OPEN_DIALOG_TRIGGER).process(
066: FileChooserHandler.init().select(files)).run();
067: ArrayUtils.assertEquals(new File[] { javaHome, userHome },
068: chooser.getSelectedFiles());
069: }
070:
071: public void testCancelSelection() throws Exception {
072: WindowInterceptor.init(SHOW_OPEN_DIALOG_TRIGGER).process(
073: FileChooserHandler.init().cancelSelection()).run();
074: assertEquals(0, chooser.getSelectedFiles().length);
075: }
076:
077: public void testAssertCurrentDirEquals() throws Exception {
078: chooser.setCurrentDirectory(javaHome);
079: WindowInterceptor.init(SHOW_OPEN_DIALOG_TRIGGER).process(
080: FileChooserHandler.init().assertCurrentDirEquals(
081: javaHome).select(javaHome)).run();
082: }
083:
084: public void testAssertCurrentDirEqualsError() throws Exception {
085: chooser.setCurrentDirectory(javaHome);
086: checkError(SHOW_OPEN_DIALOG_TRIGGER, FileChooserHandler.init()
087: .assertCurrentDirEquals(userHome), javaHome,
088: "Unexpected current directory - expected:<" + userHome
089: + "> but was:<" + javaHome + ">");
090: }
091:
092: public void testAssertIsOpenSaveDialog() throws Exception {
093: checkOk(SHOW_OPEN_DIALOG_TRIGGER, FileChooserHandler.init()
094: .assertIsOpenDialog());
095: checkError(SHOW_OPEN_DIALOG_TRIGGER, FileChooserHandler.init()
096: .assertIsSaveDialog(), javaHome,
097: "Chooser is in 'open' mode");
098:
099: checkOk(SHOW_SAVE_DIALOG_TRIGGER, FileChooserHandler.init()
100: .assertIsSaveDialog());
101: checkError(SHOW_SAVE_DIALOG_TRIGGER, FileChooserHandler.init()
102: .assertIsOpenDialog(), javaHome,
103: "Chooser is in 'save' mode");
104:
105: checkError(SHOW_CUSTOM_DIALOG_TRIGGER, FileChooserHandler
106: .init().assertIsSaveDialog(), javaHome,
107: "Chooser is in 'custom' mode");
108: checkError(SHOW_CUSTOM_DIALOG_TRIGGER, FileChooserHandler
109: .init().assertIsOpenDialog(), javaHome,
110: "Chooser is in 'custom' mode");
111: }
112:
113: public void testAssertTitleEquals() throws Exception {
114: chooser.setDialogTitle("title");
115: checkOk(SHOW_OPEN_DIALOG_TRIGGER, FileChooserHandler.init()
116: .titleEquals("title"));
117: checkError(SHOW_OPEN_DIALOG_TRIGGER, FileChooserHandler.init()
118: .titleEquals("error"), javaHome,
119: "Unexpected title - expected:<error> but was:<title>");
120: }
121:
122: public void testAssertApplyButtonTextEquals() throws Exception {
123: chooser.setApproveButtonText("text");
124: checkOk(SHOW_OPEN_DIALOG_TRIGGER, FileChooserHandler.init()
125: .assertApplyButtonTextEquals("text"));
126: checkError(SHOW_OPEN_DIALOG_TRIGGER, FileChooserHandler.init()
127: .assertApplyButtonTextEquals("other"), javaHome,
128: "Unexpected apply button text - expected:<other> but was:<text>");
129: }
130:
131: public void testAssertAcceptsFilesAndDirectories() throws Exception {
132: final int[] modes = { JFileChooser.FILES_ONLY,
133: JFileChooser.FILES_AND_DIRECTORIES,
134: JFileChooser.DIRECTORIES_ONLY };
135: final String[] messages = {
136: "The file chooser accepts files only.",
137: "The file chooser accepts both files and directories.",
138: "The file chooser accepts directories only." };
139: for (int i = 0; i < modes.length; i++) {
140: final FileChooserHandler[] interceptors = {
141: FileChooserHandler.init().assertAcceptsFilesOnly(),
142: FileChooserHandler.init()
143: .assertAcceptsFilesAndDirectories(),
144: FileChooserHandler.init()
145: .assertAcceptsDirectoriesOnly() };
146: chooser.setFileSelectionMode(modes[i]);
147: for (int j = 0; j < modes.length; j++) {
148: if (i == j) {
149: checkOk(SHOW_OPEN_DIALOG_TRIGGER, interceptors[j]);
150: } else {
151: checkError(SHOW_OPEN_DIALOG_TRIGGER,
152: interceptors[j], javaHome, messages[i]);
153: }
154: }
155: }
156: }
157:
158: public void testAssertMultiSelectionEnabled() throws Exception {
159: checkMultiSelectionEnabled(true, "Multi selection is enabled.");
160: checkMultiSelectionEnabled(false,
161: "Multi selection is not enabled.");
162: }
163:
164: public void testShownDialogIsNotAFileChooserButAJFrame()
165: throws Exception {
166: checkUnexpectedWindowShown(new JFrame("title"), "title");
167: }
168:
169: public void testShownDialogIsNotAFileChooserButAModalDialog()
170: throws Exception {
171: checkUnexpectedWindowShown(createModalDialog("aDialog"),
172: "aDialog");
173: }
174:
175: private void checkUnexpectedWindowShown(final Window window,
176: String title) {
177: checkAssertionFailedError(WindowInterceptor.init(new Trigger() {
178: public void run() throws Exception {
179: window.setVisible(true);
180: }
181: }).process(FileChooserHandler.init().select(javaHome)),
182: "The shown window is not a file chooser - window content:"
183: + Utils.LINE_SEPARATOR + "<window title=\""
184: + title + "\"/>");
185: }
186:
187: private void checkOk(Trigger trigger, FileChooserHandler handler) {
188: WindowInterceptor.init(trigger).process(
189: handler.select(javaHome)).run();
190: }
191:
192: private void checkError(Trigger trigger,
193: FileChooserHandler handler, File selectedFile,
194: String errorMessage) {
195: checkAssertionFailedError(WindowInterceptor.init(trigger)
196: .process(handler.select(selectedFile)), errorMessage);
197: }
198:
199: private void checkMultiSelectionEnabled(boolean enabled,
200: String message) {
201: chooser.setMultiSelectionEnabled(enabled);
202: checkOk(SHOW_OPEN_DIALOG_TRIGGER, FileChooserHandler.init()
203: .assertMultiSelectionEnabled(enabled));
204: checkError(SHOW_OPEN_DIALOG_TRIGGER, FileChooserHandler.init()
205: .assertMultiSelectionEnabled(!enabled), javaHome,
206: message);
207: }
208: }
|