001 /*
002 * Created on Jul 9, 2007
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 *
014 * Copyright @2007-2010 the original author or authors.
015 */
016 package org.fest.swing.fixture;
017
018 import java.io.File;
019
020 import javax.swing.JFileChooser;
021
022 import org.fest.swing.core.*;
023 import org.fest.swing.driver.JFileChooserDriver;
024 import org.fest.swing.exception.ComponentLookupException;
025 import org.fest.swing.exception.WaitTimedOutError;
026 import org.fest.swing.timing.Timeout;
027
028 /**
029 * Understands functional testing of <code>{@link JFileChooser}</code>s:
030 * <ul>
031 * <li>user input simulation</li>
032 * <li>state verification</li>
033 * <li>property value query</li>
034 * </ul>
035 *
036 * @author Yvonne Wang
037 * @author Alex Ruiz
038 */
039 public class JFileChooserFixture extends ComponentFixture<JFileChooser> implements CommonComponentFixture {
040
041 private JFileChooserDriver driver;
042
043 /**
044 * Creates a new <code>{@link JFileChooserFixture}</code>.
045 * @param robot performs simulation of user events on a <code>JFileChooser</code>.
046 * @throws NullPointerException if <code>robot</code> is <code>null</code>.
047 * @throws ComponentLookupException if a matching <code>JFileChooser</code> could not be found.
048 * @throws ComponentLookupException if more than one matching <code>JFileChooser</code> is found.
049 */
050 public JFileChooserFixture(Robot robot) {
051 super(robot, JFileChooser.class);
052 createDriver();
053 }
054
055 /**
056 * Creates a new <code>{@link JFileChooserFixture}</code>.
057 * @param robot performs simulation of user events on the given <code>JFileChooser</code>.
058 * @param target the <code>JFileChooser</code> to be managed by this fixture.
059 * @throws NullPointerException if <code>robot</code> is <code>null</code>.
060 * @throws NullPointerException if <code>target</code> is <code>null</code>.
061 */
062 public JFileChooserFixture(Robot robot, JFileChooser target) {
063 super(robot, target);
064 createDriver();
065 }
066
067 /**
068 * Creates a new <code>{@link JFileChooserFixture}</code>.
069 * @param robot performs simulation of user events on a <code>JFileChooser</code>.
070 * @param fileChooserName the name of the <code>JFileChooser</code> to find using the given <code>RobotFixture</code>.
071 * @throws NullPointerException if <code>robot</code> is <code>null</code>.
072 * @throws ComponentLookupException if a matching <code>JFileChooser</code> could not be found.
073 * @throws ComponentLookupException if more than one matching <code>JFileChooser</code> is found.
074 */
075 public JFileChooserFixture(Robot robot, String fileChooserName) {
076 super(robot, fileChooserName, JFileChooser.class);
077 createDriver();
078 }
079
080 private void createDriver() {
081 driver(new JFileChooserDriver(robot));
082 }
083
084 /**
085 * Sets the <code>{@link JFileChooserDriver}</code> to be used by this fixture.
086 * @param newDriver the new <code>JFileChooserDriver</code>.
087 * @throws NullPointerException if the given driver is <code>null</code>.
088 */
089 protected final void driver(JFileChooserDriver newDriver) {
090 validateNotNull(newDriver);
091 driver = newDriver;
092 }
093
094 /**
095 * Simulates a user pressing the "Approve" button in this fixture's <code>{@link JFileChooser}</code>.
096 * @throws ComponentLookupException if the "Approve" button cannot be found.
097 * @throws IllegalStateException if the "Approve" button is disabled.
098 * @throws IllegalStateException if the "Approve" button is not showing on the screen.
099 */
100 public void approve() {
101 driver.clickApproveButton(target);
102 }
103
104 /**
105 * Finds the "Approve" button in this fixture's <code>{@link JFileChooser}</code>.
106 * @return the found "Approve" button.
107 * @throws ComponentLookupException if the "Approve" button cannot be found.
108 */
109 public JButtonFixture approveButton() {
110 return new JButtonFixture(robot, driver.approveButton(target));
111 }
112
113 /**
114 * Simulates a user pressing the "Cancel" button in this fixture's <code>{@link JFileChooser}</code>.
115 * @throws ComponentLookupException if the "Cancel" button cannot be found.
116 * @throws IllegalStateException if the "Cancel" button is disabled.
117 * @throws IllegalStateException if the "Cancel" button is not showing on the screen.
118 */
119 public void cancel() {
120 driver.clickCancelButton(target);
121 }
122
123 /**
124 * Finds the "Cancel" button in this fixture's <code>{@link JFileChooser}</code>.
125 * @return the found "Cancel" button.
126 * @throws ComponentLookupException if the "Cancel" button cannot be found.
127 */
128 public JButtonFixture cancelButton() {
129 return new JButtonFixture(robot, driver.cancelButton(target));
130 }
131
132 /**
133 * Returns a fixture that manages the field where the user can enter the name of the file to select in this fixture's
134 * <code>{@link JFileChooser}</code>.
135 * @return the created fixture.
136 * @throws ComponentLookupException if a matching textToMatch field could not be found.
137 */
138 public JTextComponentFixture fileNameTextBox() {
139 return new JTextComponentFixture(robot, driver.fileNameTextBox(target));
140 }
141
142
143 /**
144 * Selects the given file in this fixture's <code>{@link JFileChooser}</code>.
145 * @param file the file to select.
146 * @return this fixture.
147 * @throws NullPointerException if the given file is <code>null</code>.
148 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is disabled.
149 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is not showing on the screen.
150 * @throws IllegalArgumentException if this fixture's <code>JFileChooser</code> can select directories only and the
151 * file to select is not a directory.
152 * @throws IllegalArgumentException if this fixture's <code>JFileChooser</code> cannot select directories and the file
153 * to select is a directory.
154 */
155 public JFileChooserFixture selectFile(final File file) {
156 driver.selectFile(target, file);
157 return this;
158 }
159
160
161 /**
162 * Selects the given files in this fixture's <code>{@link JFileChooser}</code>.
163 * @param files the files to select.
164 * @return this fixture.
165 * @throws NullPointerException if the given array of files is <code>null</code>.
166 * @throws IllegalArgumentException if the given array of files is empty.
167 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is disabled.
168 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is not showing on the screen.
169 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> does not support multiple selection and
170 * there is more than one file to select.
171 * @throws IllegalArgumentException if this fixture's <code>JFileChooser</code> can select directories only and any of
172 * the files to select is not a directory.
173 * @throws IllegalArgumentException if this fixture's <code>JFileChooser</code> cannot select directories and any of
174 * the files to select is a directory.
175 */
176 public JFileChooserFixture selectFiles(File... files) {
177 driver.selectFiles(target, files);
178 return this;
179 }
180
181 /**
182 * Sets the current directory of this fixture's <code>{@link JFileChooser}</code> to the given one.
183 * @param dir the directory to set as current.
184 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is disabled.
185 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is not showing on the screen.
186 * @return this fixture.
187 */
188 public JFileChooserFixture setCurrentDirectory(final File dir) {
189 driver.setCurrentDirectory(target, dir);
190 return this;
191 }
192
193 /**
194 * Simulates a user clicking this fixture's <code>{@link JFileChooser}</code>.
195 * @return this fixture.
196 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is disabled.
197 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is not showing on the screen.
198 */
199 public JFileChooserFixture click() {
200 driver.click(target);
201 return this;
202 }
203
204 /**
205 * Simulates a user clicking this fixture's <code>{@link JFileChooser}</code>.
206 * @param button the button to click.
207 * @return this fixture.
208 * @throws NullPointerException if the given <code>MouseButton</code> is <code>null</code>.
209 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is disabled.
210 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is not showing on the screen.
211 */
212 public JFileChooserFixture click(MouseButton button) {
213 driver.click(target, button);
214 return this;
215 }
216
217 /**
218 * Simulates a user clicking this fixture's <code>{@link JFileChooser}</code>.
219 * @param mouseClickInfo specifies the button to click and the times the button should be clicked.
220 * @return this fixture.
221 * @throws NullPointerException if the given <code>MouseClickInfo</code> is <code>null</code>.
222 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is disabled.
223 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is not showing on the screen.
224 */
225 public JFileChooserFixture click(MouseClickInfo mouseClickInfo) {
226 driver.click(target, mouseClickInfo);
227 return this;
228 }
229
230 /**
231 * Simulates a user double-clicking this fixture's <code>{@link JFileChooser}</code>.
232 * @return this fixture.
233 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is disabled.
234 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is not showing on the screen.
235 */
236 public JFileChooserFixture doubleClick() {
237 driver.doubleClick(target);
238 return this;
239 }
240
241 /**
242 * Simulates a user right-clicking this fixture's <code>{@link JFileChooser}</code>.
243 * @return this fixture.
244 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is disabled.
245 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is not showing on the screen.
246 */
247 public JFileChooserFixture rightClick() {
248 driver.rightClick(target);
249 return this;
250 }
251
252 /**
253 * Gives input focus to this fixture's <code>{@link JFileChooser}</code>.
254 * @return this fixture.
255 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is disabled.
256 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is not showing on the screen.
257 */
258 public JFileChooserFixture focus() {
259 driver.focus(target);
260 return this;
261 }
262
263 /**
264 * Simulates a user pressing given key with the given modifiers on this fixture's <code>{@link JFileChooser}</code>.
265 * Modifiers is a mask from the available <code>{@link java.awt.event.InputEvent}</code> masks.
266 * @param keyPressInfo specifies the key and modifiers to press.
267 * @return this fixture.
268 * @throws NullPointerException if the given <code>KeyPressInfo</code> is <code>null</code>.
269 * @throws IllegalArgumentException if the given code is not a valid key code.
270 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is disabled.
271 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is not showing on the screen.
272 * @see KeyPressInfo
273 */
274 public JFileChooserFixture pressAndReleaseKey(KeyPressInfo keyPressInfo) {
275 driver.pressAndReleaseKey(target, keyPressInfo);
276 return this;
277 }
278
279 /**
280 * Simulates a user pressing and releasing the given keys on the <code>{@link JFileChooser}</code> managed by this
281 * fixture.
282 * @param keyCodes one or more codes of the keys to press.
283 * @return this fixture.
284 * @throws NullPointerException if the given array of codes is <code>null</code>.
285 * @throws IllegalArgumentException if any of the given code is not a valid key code.
286 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is disabled.
287 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is not showing on the screen.
288 * @see java.awt.event.KeyEvent
289 */
290 public JFileChooserFixture pressAndReleaseKeys(int... keyCodes) {
291 driver.pressAndReleaseKeys(target, keyCodes);
292 return this;
293 }
294
295 /**
296 * Simulates a user pressing the given key on this fixture's <code>{@link JFileChooser}</code>.
297 * @param keyCode the code of the key to press.
298 * @return this fixture.
299 * @throws IllegalArgumentException if any of the given code is not a valid key code.
300 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is disabled.
301 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is not showing on the screen.
302 * @see java.awt.event.KeyEvent
303 */
304 public JFileChooserFixture pressKey(int keyCode) {
305 driver.pressKey(target, keyCode);
306 return this;
307 }
308
309 /**
310 * Simulates a user releasing the given key on this fixture's <code>{@link JFileChooser}</code>.
311 * @param keyCode the code of the key to release.
312 * @return this fixture.
313 * @throws IllegalArgumentException if any of the given code is not a valid key code.
314 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is disabled.
315 * @throws IllegalStateException if this fixture's <code>JFileChooser</code> is not showing on the screen.
316 * @see java.awt.event.KeyEvent
317 */
318 public JFileChooserFixture releaseKey(int keyCode) {
319 driver.releaseKey(target, keyCode);
320 return this;
321 }
322
323 /**
324 * Asserts that this fixture's <code>{@link JFileChooser}</code> has input focus.
325 * @return this fixture.
326 * @throws AssertionError if this fixture's <code>JFileChooser</code> does not have input focus.
327 */
328 public JFileChooserFixture requireFocused() {
329 driver.requireFocused(target);
330 return this;
331 }
332
333 /**
334 * Asserts that this fixture's <code>{@link JFileChooser}</code> is enabled.
335 * @return this fixture.
336 * @throws AssertionError if this fixture's <code>JFileChooser</code> is disabled.
337 */
338 public JFileChooserFixture requireEnabled() {
339 driver.requireEnabled(target);
340 return this;
341 }
342
343 /**
344 * Asserts that this fixture's <code>{@link JFileChooser}</code> is enabled.
345 * @param timeout the time this fixture will wait for the component to be enabled.
346 * @return this fixture.
347 * @throws WaitTimedOutError if this fixture's <code>JFileChooser</code> is never enabled.
348 */
349 public JFileChooserFixture requireEnabled(Timeout timeout) {
350 driver.requireEnabled(target, timeout);
351 return this;
352 }
353
354 /**
355 * Asserts that this fixture's <code>{@link JFileChooser}</code> is disabled.
356 * @return this fixture.
357 * @throws AssertionError if this fixture's <code>JFileChooser</code> is enabled.
358 */
359 public JFileChooserFixture requireDisabled() {
360 driver.requireDisabled(target);
361 return this;
362 }
363
364 /**
365 * Asserts that this fixture's <code>{@link JFileChooser}</code> is visible.
366 * @return this fixture.
367 * @throws AssertionError if this fixture's <code>JFileChooser</code> is not visible.
368 */
369 public JFileChooserFixture requireVisible() {
370 driver.requireVisible(target);
371 return this;
372 }
373
374 /**
375 * Asserts that this fixture's <code>{@link JFileChooser}</code> is not visible.
376 * @return this fixture.
377 * @throws AssertionError if this fixture's <code>JFileChooser</code> is visible.
378 */
379 public JFileChooserFixture requireNotVisible() {
380 driver.requireNotVisible(target);
381 return this;
382 }
383 }