001    /*
002     * Created on Dec 8, 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.awt.Dimension;
019    import java.awt.Point;
020    import java.util.regex.Pattern;
021    
022    import javax.swing.JInternalFrame;
023    
024    import org.fest.swing.core.*;
025    import org.fest.swing.driver.JInternalFrameDriver;
026    import org.fest.swing.exception.*;
027    import org.fest.swing.timing.Timeout;
028    
029    /**
030     * Understands functional testing of <code>{@link JInternalFrame}</code>s:
031     * <ul>
032     * <li>user input simulation</li>
033     * <li>state verification</li>
034     * <li>property value query</li>
035     * </ul>
036     *
037     * @author Alex Ruiz
038     */
039    public class JInternalFrameFixture extends ContainerFixture<JInternalFrame> implements CommonComponentFixture,
040        FrameLikeFixture, JComponentFixture, JPopupMenuInvokerFixture {
041    
042      private JInternalFrameDriver driver;
043    
044      /**
045       * Creates a new <code>{@link JInternalFrameFixture}</code>.
046       * @param robot performs simulation of user events on a <code>JInternalFrame</code>.
047       * @param internalFrameName the name of the <code>JInternalFrame</code> to find using the given <code>Robot</code>.
048       * @throws NullPointerException if <code>robot</code> is <code>null</code>.
049       * @throws ComponentLookupException if a matching <code>JInternalFrame</code> could not be found.
050       * @throws ComponentLookupException if more than one matching <code>JInternalFrame</code> is found.
051       */
052      public JInternalFrameFixture(Robot robot, String internalFrameName) {
053        super(robot, internalFrameName, JInternalFrame.class);
054        createDriver();
055      }
056    
057      /**
058       * Creates a new <code>{@link JInternalFrameFixture}</code>.
059       * @param robot performs simulation of user events on the given <code>JInternalFrame</code>.
060       * @param target the <code>JInternalFrame</code> to be managed by this fixture.
061       * @throws NullPointerException if <code>robot</code> is <code>null</code>.
062       * @throws NullPointerException if <code>target</code> is <code>null</code>.
063       */
064      public JInternalFrameFixture(Robot robot, JInternalFrame target) {
065        super(robot, target);
066        createDriver();
067      }
068    
069      private void createDriver() {
070        driver(new JInternalFrameDriver(robot));
071      }
072    
073      /**
074       * Sets the <code>{@link JInternalFrameDriver}</code> to be used by this fixture.
075       * @param newDriver the new <code>JInternalFrameDriver</code>.
076       * @throws NullPointerException if the given driver is <code>null</code>.
077       */
078      protected final void driver(JInternalFrameDriver newDriver) {
079        validateNotNull(newDriver);
080        driver = newDriver;
081      }
082    
083      /**
084       * Brings this fixture's <code>{@link JInternalFrame}</code> to the front.
085       * @return this fixture.
086       */
087      public JInternalFrameFixture moveToFront() {
088        driver.moveToFront(target);
089        return this;
090      }
091    
092      /**
093       * Brings this fixture's <code>{@link JInternalFrame}</code> to the back.
094       * @return this fixture.
095       */
096      public JInternalFrameFixture moveToBack() {
097        driver.moveToBack(target);
098        return this;
099      }
100    
101      /**
102       * Simulates a user deiconifying this fixture's <code>{@link JInternalFrame}</code>.
103       * @return this fixture.
104       * @throws ActionFailedException if the <code>JInternalFrame</code> vetoes the action.
105       */
106      public JInternalFrameFixture deiconify() {
107        driver.deiconify(target);
108        return this;
109      }
110    
111      /**
112       * Simulates a user iconifying this fixture's <code>{@link JInternalFrame}</code>.
113       * @return this fixture.
114       * @throws ActionFailedException if the given <code>JInternalFrame</code> is not iconifiable.
115       * @throws ActionFailedException if the <code>JInternalFrame</code> vetoes the action.
116       */
117      public JInternalFrameFixture iconify() {
118        driver.iconify(target);
119        return this;
120      }
121    
122      /**
123       * Simulates a user maximizing this fixture's <code>{@link JInternalFrame}</code>, deconifying it first if it is
124       * iconified.
125       * @return this fixture.
126       * @throws ActionFailedException if the given <code>JInternalFrame</code> is not maximizable.
127       * @throws ActionFailedException if the <code>JInternalFrame</code> vetoes the action.
128       */
129      public JInternalFrameFixture maximize() {
130        driver.maximize(target);
131        return this;
132      }
133    
134      /**
135       * Simulates a user normalizing this fixture's <code>{@link JInternalFrame}</code>, deconifying it first if it is
136       * iconified.
137       * @return this fixture.
138       * @throws ActionFailedException if the <code>JInternalFrame</code> vetoes the action.
139       */
140      public JInternalFrameFixture normalize() {
141        driver.normalize(target);
142        return this;
143      }
144    
145      /**
146       * Simulates a user closing this fixture's <code>{@link JInternalFrame}</code>.
147       * @throws ActionFailedException if the <code>JInternalFrame</code> is not closable.
148       */
149      public void close() {
150        driver.close(target);
151      }
152    
153      /**
154       * Asserts that the size of this fixture's <code>{@link JInternalFrame}</code> is equal to given one.
155       * @param size the given size to match.
156       * @return this fixture.
157       * @throws AssertionError if the size of this fixture's <code>JInternalFrame</code> is not equal to the given size.
158       */
159      public JInternalFrameFixture requireSize(Dimension size) {
160        driver.requireSize(target, size);
161        return this;
162      }
163    
164      /**
165       * Simulates a user resizing horizontally this fixture's <code>{@link JInternalFrame}</code>.
166       * @param width the width that this fixture's <code>JInternalFrame</code> should have after being resized.
167       * @return this fixture.
168       */
169      public JInternalFrameFixture resizeWidthTo(int width) {
170        driver.resizeWidthTo(target, width);
171        return this;
172      }
173    
174      /**
175       * Simulates a user resizing vertically this fixture's <code>{@link JInternalFrame}</code>.
176       * @param height the height that this fixture's <code>JInternalFrame</code> should have after being resized.
177       * @return this fixture.
178       */
179      public JInternalFrameFixture resizeHeightTo(int height) {
180        driver.resizeHeightTo(target, height);
181        return this;
182      }
183    
184      /**
185       * Simulates a user resizing this fixture's <code>{@link JInternalFrame}</code>.
186       * @param size the size that the target <code>JInternalFrame</code> should have after being resized.
187       * @return this fixture.
188       */
189      public JInternalFrameFixture resizeTo(Dimension size) {
190        driver.resizeTo(target, size);
191        return this;
192      }
193    
194      /**
195       * Simulates a user moving this fixture's <code>{@link JInternalFrame}</code> to the given point.
196       * @param p the point to move this fixture's <code>JInternalFrame</code> to.
197       * @return this fixture.
198       */
199      public JInternalFrameFixture moveTo(Point p) {
200        driver.moveTo(target, p);
201        return this;
202      }
203    
204      /**
205       * Simulates a user clicking this fixture's <code>{@link JInternalFrame}</code>.
206       * @return this fixture.
207       */
208      public JInternalFrameFixture click() {
209        driver.click(target);
210        return this;
211      }
212    
213      /**
214       * Simulates a user clicking this fixture's <code>{@link JInternalFrame}</code>.
215       * @param button the button to click.
216       * @return this fixture.
217       */
218      public JInternalFrameFixture click(MouseButton button) {
219        driver.click(target, button);
220        return this;
221      }
222    
223      /**
224       * Simulates a user clicking this fixture's <code>{@link JInternalFrame}</code>.
225       * @param mouseClickInfo specifies the button to click and the times the button should be clicked.
226       * @return this fixture.
227       * @throws NullPointerException if the given <code>MouseClickInfo</code> is <code>null</code>.
228       */
229      public JInternalFrameFixture click(MouseClickInfo mouseClickInfo) {
230        driver.click(target, mouseClickInfo);
231        return this;
232      }
233    
234      /**
235       * Simulates a user right-clicking this fixture's <code>{@link JInternalFrame}</code>.
236       * @return this fixture.
237       */
238      public JInternalFrameFixture rightClick() {
239        driver.rightClick(target);
240        return this;
241      }
242    
243      /**
244       * Simulates a user double-clicking this fixture's <code>{@link JInternalFrame}</code>.
245       * @return this fixture.
246       */
247      public JInternalFrameFixture doubleClick() {
248        driver.doubleClick(target);
249        return this;
250      }
251    
252      /**
253       * Gives input focus to this fixture's <code>{@link JInternalFrame}</code>.
254       * @return this fixture.
255       */
256      public JInternalFrameFixture focus() {
257        driver.focus(target);
258        return this;
259      }
260    
261      /**
262       * Simulates a user pressing given key with the given modifiers on this fixture's <code>{@link JInternalFrame}</code>.
263       * Modifiers is a mask from the available <code>{@link java.awt.event.InputEvent}</code> masks.
264       * @param keyPressInfo specifies the key and modifiers to press.
265       * @return this fixture.
266       * @throws NullPointerException if the given <code>KeyPressInfo</code> is <code>null</code>.
267       * @throws IllegalArgumentException if the given code is not a valid key code.
268       * @see KeyPressInfo
269       */
270      public JInternalFrameFixture pressAndReleaseKey(KeyPressInfo keyPressInfo) {
271        driver.pressAndReleaseKey(target, keyPressInfo);
272        return this;
273      }
274    
275      /**
276       * Simulates a user pressing and releasing the given keys on this fixture's <code>{@link JInternalFrame}</code> .
277       * @param keyCodes one or more codes of the keys to press.
278       * @return this fixture.
279       * @throws NullPointerException if the given array of codes is <code>null</code>.
280       * @throws IllegalArgumentException if any of the given code is not a valid key code.
281       * @see java.awt.event.KeyEvent
282       */
283      public JInternalFrameFixture pressAndReleaseKeys(int... keyCodes) {
284        driver.pressAndReleaseKeys(target, keyCodes);
285        return this;
286      }
287    
288      /**
289       * Simulates a user pressing given key on this fixture's <code>{@link JInternalFrame}</code>.
290       * @param keyCode the code of the key to press.
291       * @return this fixture.
292       * @throws IllegalArgumentException if any of the given code is not a valid key code.
293       * @see java.awt.event.KeyEvent
294       */
295      public JInternalFrameFixture pressKey(int keyCode) {
296        driver.pressKey(target, keyCode);
297        return this;
298      }
299    
300      /**
301       * Simulates a user releasing the given key on this fixture's <code>{@link JInternalFrame}</code>.
302       * @param keyCode the code of the key to release.
303       * @return this fixture.
304       * @throws IllegalArgumentException if any of the given code is not a valid key code.
305       * @see java.awt.event.KeyEvent
306       */
307      public JInternalFrameFixture releaseKey(int keyCode) {
308        driver.releaseKey(target, keyCode);
309        return this;
310      }
311    
312      /**
313       * Asserts that this fixture's <code>{@link JInternalFrame}</code> has input focus.
314       * @return this fixture.
315       * @throws AssertionError if this fixture's <code>JInternalFrame</code> does not have input focus.
316       */
317      public JInternalFrameFixture requireFocused() {
318        driver.requireFocused(target);
319        return this;
320      }
321    
322      /**
323       * Asserts that this fixture's <code>{@link JInternalFrame}</code> is enabled.
324       * @return this fixture.
325       * @throws AssertionError if the managed <code>JInternalFrame</code> is disabled.
326       */
327      public JInternalFrameFixture requireEnabled() {
328        driver.requireEnabled(target);
329        return this;
330      }
331    
332      /**
333       * Asserts that this fixture's <code>{@link JInternalFrame}</code> is enabled.
334       * @param timeout the time this fixture will wait for the component to be enabled.
335       * @return this fixture.
336       * @throws WaitTimedOutError if the managed <code>JInternalFrame</code> is never enabled.
337       */
338      public JInternalFrameFixture requireEnabled(Timeout timeout) {
339        driver.requireEnabled(target, timeout);
340        return this;
341      }
342    
343      /**
344       * Asserts that this fixture's <code>{@link JInternalFrame}</code> is disabled.
345       * @return this fixture.
346       * @throws AssertionError if the managed <code>JInternalFrame</code> is enabled.
347       */
348      public JInternalFrameFixture requireDisabled() {
349        driver.requireDisabled(target);
350        return this;
351      }
352    
353      /**
354       * Asserts that this fixture's <code>{@link JInternalFrame}</code> is visible.
355       * @return this fixture.
356       * @throws AssertionError if the managed <code>JInternalFrame</code> is not visible.
357       */
358      public JInternalFrameFixture requireVisible() {
359        driver.requireVisible(target);
360        return this;
361      }
362    
363      /**
364       * Asserts that this fixture's <code>{@link JInternalFrame}</code> is not visible.
365       * @return this fixture.
366       * @throws AssertionError if the managed <code>JInternalFrame</code> is visible.
367       */
368      public JInternalFrameFixture requireNotVisible() {
369        driver.requireNotVisible(target);
370        return this;
371      }
372    
373    
374      /**
375       * Asserts that the toolTip in this fixture's <code>{@link JInternalFrame}</code> matches the given value.
376       * @param expected the given value. It can be a regular expression.
377       * @return this fixture.
378       * @throws AssertionError if the toolTip in this fixture's <code>JInternalFrame</code> does not match the given value.
379       * @since 1.2
380       */
381      public JInternalFrameFixture requireToolTip(String expected) {
382        driver.requireToolTip(target, expected);
383        return this;
384      }
385    
386      /**
387       * Asserts that the toolTip in this fixture's <code>{@link JInternalFrame}</code> matches the given regular expression
388       * pattern.
389       * @param pattern the regular expression pattern to match.
390       * @return this fixture.
391       * @throws NullPointerException if the given regular expression pattern is <code>null</code>.
392       * @throws AssertionError if the toolTip in this fixture's <code>JInternalFrame</code> does not match the given
393       * regular expression.
394       * @since 1.2
395       */
396      public JInternalFrameFixture requireToolTip(Pattern pattern) {
397        driver.requireToolTip(target, pattern);
398        return this;
399      }
400    
401      /**
402       * Returns the client property stored in this fixture's <code>{@link JInternalFrame}</code>, under the given key.
403       * @param key the key to use to retrieve the client property.
404       * @return the value of the client property stored under the given key, or <code>null</code> if the property was
405       * not found.
406       * @throws NullPointerException if the given key is <code>null</code>.
407       * @since 1.2
408       */
409      public Object clientProperty(Object key) {
410        return driver.clientProperty(target, key);
411      }
412    
413      /**
414       * Shows a pop-up menu using this fixture's <code>{@link JInternalFrame}</code> as the invoker of the pop-up menu.
415       * @return a fixture that manages the displayed pop-up menu.
416       * @throws IllegalStateException if this fixture's <code>JInternalFrame</code> is disabled.
417       * @throws IllegalStateException if this fixture's <code>JInternalFrame</code> is not showing on the screen.
418       * @throws ComponentLookupException if a pop-up menu cannot be found.
419       */
420      public JPopupMenuFixture showPopupMenu() {
421        return new JPopupMenuFixture(robot, driver.invokePopupMenu(target));
422      }
423    
424      /**
425       * Shows a pop-up menu at the given point using this fixture's <code>{@link JInternalFrame}</code> as the invoker of
426       * the pop-up menu.
427       * @param p the given point where to show the pop-up menu.
428       * @return a fixture that manages the displayed pop-up menu.
429       * @throws IllegalStateException if this fixture's <code>JInternalFrame</code> is disabled.
430       * @throws IllegalStateException if this fixture's <code>JInternalFrame</code> is not showing on the screen.
431       * @throws ComponentLookupException if a pop-up menu cannot be found.
432       */
433      public JPopupMenuFixture showPopupMenuAt(Point p) {
434        return new JPopupMenuFixture(robot, driver.invokePopupMenu(target, p));
435      }
436    }