001: package org.uispec4j.interception;
002:
003: import junit.framework.Assert;
004: import junit.framework.AssertionFailedError;
005: import org.uispec4j.Trigger;
006: import org.uispec4j.Window;
007: import org.uispec4j.utils.ComponentUtils;
008: import org.uispec4j.utils.Utils;
009:
010: import javax.swing.*;
011: import java.awt.*;
012: import java.io.File;
013: import java.util.ArrayList;
014: import java.util.Iterator;
015: import java.util.List;
016:
017: /**
018: * WindowHandler implementation dedicated to file chooser dialogs.<p>
019: * Sample usage:
020: * <pre><code>
021: * WindowInterceptor
022: * .run(openMenu.triggerClick())
023: * .process(FileChooserHandler.init()
024: * .titleEquals("Select a template file")
025: * .assertAcceptsFilesOnly()
026: * .select("/home/bond/file.txt"))
027: * .run();
028: * </code></pre>
029: *
030: * @see <a href="http://www.uispec4j.org/interception.html">Intercepting windows</a>
031: */
032: public class FileChooserHandler {
033: private DialogHandler handler = new DialogHandler();
034:
035: /**
036: * Creates a new interceptor with the associated {@link Trigger}
037: */
038: public static FileChooserHandler init() {
039: return new FileChooserHandler();
040: }
041:
042: public FileChooserHandler assertIsOpenDialog() {
043: handler.add(new DialogTypeFileChooserHandler(
044: JFileChooser.OPEN_DIALOG));
045: return this ;
046: }
047:
048: public FileChooserHandler assertIsSaveDialog() {
049: handler.add(new DialogTypeFileChooserHandler(
050: JFileChooser.SAVE_DIALOG));
051: return this ;
052: }
053:
054: public FileChooserHandler assertCurrentDirEquals(
055: final File currentDir) {
056: handler.add(new FileChooserInternalHandler() {
057: public void process(JFileChooser fileChooser) {
058: Assert.assertEquals("Unexpected current directory -",
059: currentDir, fileChooser.getCurrentDirectory());
060: }
061: });
062: return this ;
063: }
064:
065: public FileChooserHandler titleEquals(final String title) {
066: handler.add(new FileChooserInternalHandler() {
067: public void process(JFileChooser fileChooser) {
068: Assert.assertEquals("Unexpected title -", title,
069: fileChooser.getDialogTitle());
070: }
071: });
072: return this ;
073: }
074:
075: public FileChooserHandler assertApplyButtonTextEquals(
076: final String text) {
077: handler.add(new FileChooserInternalHandler() {
078: public void process(JFileChooser fileChooser) {
079: Assert.assertEquals("Unexpected apply button text -",
080: text, fileChooser.getApproveButtonText());
081: }
082: });
083: return this ;
084: }
085:
086: public FileChooserHandler assertAcceptsFilesOnly() {
087: handler.add(new FileSelectionModeFileChooserHandler(
088: JFileChooser.FILES_ONLY));
089: return this ;
090: }
091:
092: public FileChooserHandler assertAcceptsFilesAndDirectories() {
093: handler.add(new FileSelectionModeFileChooserHandler(
094: JFileChooser.FILES_AND_DIRECTORIES));
095: return this ;
096: }
097:
098: public FileChooserHandler assertAcceptsDirectoriesOnly() {
099: handler.add(new FileSelectionModeFileChooserHandler(
100: JFileChooser.DIRECTORIES_ONLY));
101: return this ;
102: }
103:
104: public FileChooserHandler assertMultiSelectionEnabled(
105: final boolean enabled) {
106: handler.add(new FileChooserInternalHandler() {
107: public void process(JFileChooser fileChooser) {
108: boolean actual = fileChooser.isMultiSelectionEnabled();
109: if (actual != enabled) {
110: Assert.fail(actual ? "Multi selection is enabled."
111: : "Multi selection is not enabled.");
112: }
113: }
114: });
115: return this ;
116: }
117:
118: public WindowHandler select(final File file) {
119: handler.add(new FileChooserInternalHandler() {
120: public void process(JFileChooser fileChooser) {
121: fileChooser.setSelectedFile(file);
122: }
123: });
124: return handler;
125: }
126:
127: public WindowHandler select(final File[] files) {
128: handler.add(new FileChooserInternalHandler() {
129: public void process(JFileChooser fileChooser) {
130: fileChooser.setSelectedFiles(files);
131: }
132: });
133: return handler;
134: }
135:
136: public WindowHandler select(String fileName) {
137: return select(new File(fileName));
138: }
139:
140: public WindowHandler select(String[] fileNames) {
141: File[] files = new File[fileNames.length];
142: for (int i = 0; i < files.length; i++) {
143: files[i] = new File(fileNames[i]);
144: }
145: return select(files);
146: }
147:
148: /** Clicks on "Cancel". */
149: public WindowHandler cancelSelection() {
150: handler.add(new FileChooserInternalHandler() {
151: public void process(JFileChooser fileChooser) {
152: fileChooser.cancelSelection();
153: }
154: });
155: return handler;
156: }
157:
158: private static class DialogHandler extends WindowHandler {
159: private List fileChooserHandlers = new ArrayList();
160:
161: public DialogHandler() {
162: super ("FileChooserHandler");
163: }
164:
165: void add(FileChooserInternalHandler handler) {
166: fileChooserHandlers.add(handler);
167: }
168:
169: public Trigger process(final Window window) {
170: Component[] components = window
171: .getSwingComponents(JFileChooser.class);
172: if (components.length != 1) {
173: throw new AssertionFailedError(
174: "The shown window is not a file chooser - window content:"
175: + Utils.LINE_SEPARATOR
176: + window.getDescription());
177: }
178: JFileChooser fileChooser = (JFileChooser) components[0];
179: for (Iterator iterator = fileChooserHandlers.iterator(); iterator
180: .hasNext();) {
181: FileChooserInternalHandler handler = (FileChooserInternalHandler) iterator
182: .next();
183: handler.process(fileChooser);
184: }
185: fileChooser.approveSelection();
186: return new Trigger() {
187: public void run() throws Exception {
188: ComponentUtils.close(window);
189: }
190: };
191: }
192: }
193:
194: private interface FileChooserInternalHandler {
195: void process(JFileChooser fileChooser);
196: }
197:
198: private static class DialogTypeFileChooserHandler implements
199: FileChooserInternalHandler {
200: int expectedType;
201:
202: public DialogTypeFileChooserHandler(int expectedType) {
203: this .expectedType = expectedType;
204: }
205:
206: public void process(JFileChooser fileChooser) {
207: if (fileChooser.getDialogType() != expectedType) {
208: Assert.fail(getChooserType(fileChooser));
209: }
210: }
211:
212: private String getChooserType(JFileChooser fileChooser) {
213: String message = "Chooser is in '";
214: switch (fileChooser.getDialogType()) {
215: case JFileChooser.OPEN_DIALOG:
216: message += "open";
217: break;
218: case JFileChooser.SAVE_DIALOG:
219: message += "save";
220: break;
221: default:
222: message += "custom";
223: }
224: message += "' mode";
225: return message;
226: }
227: }
228:
229: private static class FileSelectionModeFileChooserHandler implements
230: FileChooserInternalHandler {
231: int expectedMode;
232:
233: public FileSelectionModeFileChooserHandler(int expectedMode) {
234: this .expectedMode = expectedMode;
235: }
236:
237: public void process(JFileChooser fileChooser) {
238: int actualMode = fileChooser.getFileSelectionMode();
239: if (actualMode != expectedMode) {
240: Assert.fail(getMessage(actualMode));
241: }
242: }
243:
244: private String getMessage(int mode) {
245: String message = "The file chooser accepts ";
246: switch (mode) {
247: case JFileChooser.FILES_ONLY:
248: message += "files only.";
249: break;
250: case JFileChooser.DIRECTORIES_ONLY:
251: message += "directories only.";
252: break;
253: case JFileChooser.FILES_AND_DIRECTORIES:
254: message += "both files and directories.";
255: }
256: return message;
257: }
258: }
259: }
|