001    /*
002     * Created on Jan 27, 2008
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005     * 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 is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     *
013     * Copyright @2008-2010 the original author or authors.
014     */
015    package org.fest.swing.driver;
016    
017    import static org.fest.swing.driver.ComponentStateValidator.validateIsEnabledAndShowing;
018    import static org.fest.swing.driver.WindowLikeContainerLocations.closeLocationOf;
019    import static org.fest.swing.edt.GuiActionRunner.execute;
020    
021    import java.awt.*;
022    
023    import org.fest.swing.annotation.RunsInEDT;
024    import org.fest.swing.core.Robot;
025    import org.fest.swing.edt.GuiQuery;
026    import org.fest.swing.edt.GuiTask;
027    import org.fest.swing.exception.ActionFailedException;
028    
029    /**
030     * Understands functional testing of <code>{@link Window}</code>s:
031     * <ul>
032     * <li>user input simulation</li>
033     * <li>state verification</li>
034     * <li>property value query</li>
035     * </ul>
036     * This class is intended for internal use only. Please use the classes in the package
037     * <code>{@link org.fest.swing.fixture}</code> in your tests.
038     *
039     * @author Alex Ruiz
040     */
041    public class WindowDriver extends ContainerDriver {
042    
043      /**
044       * Creates a new </code>{@link WindowDriver}</code>.
045       * @param robot the robot to use to simulate user input.
046       */
047      public WindowDriver(Robot robot) {
048        super(robot);
049      }
050    
051      /**
052       * Resizes the <code>{@link Window}</code> horizontally.
053       * @param w the target <code>Window</code>.
054       * @param width the width that the <code>Window</code> should have after being resized.
055       * @throws ActionFailedException if the <code>Window</code> is not enabled.
056       * @throws ActionFailedException if the <code>Window</code> is not resizable by the user.
057       * @throws ActionFailedException if the <code>Window</code> is not showing on the screen.
058       */
059      @RunsInEDT
060      public void resizeWidthTo(Window w, int width) {
061        resizeWidth(w, width);
062      }
063    
064      /**
065       * Resizes the <code>{@link Window}</code> vertically.
066       * @param w the target <code>Window</code>.
067       * @param height the height that the <code>Window</code> should have after being resized.
068       * @throws ActionFailedException if the <code>Window</code> is not enabled.
069       * @throws ActionFailedException if the <code>Window</code> is not resizable by the user.
070       * @throws ActionFailedException if the <code>Window</code> is not showing on the screen.
071       */
072      @RunsInEDT
073      public void resizeHeightTo(Window w, int height) {
074        resizeHeight(w, height);
075      }
076    
077      /**
078       * Resizes the <code>{@link Window}</code> to the given size.
079       * @param w the target <code>Window</code>.
080       * @param size the size to resize the <code>Window</code> to.
081       * @throws ActionFailedException if the <code>Window</code> is not enabled.
082       * @throws ActionFailedException if the <code>Window</code> is not resizable by the user.
083       * @throws ActionFailedException if the <code>Window</code> is not showing on the screen.
084       */
085      @RunsInEDT
086      public void resizeTo(Window w, Dimension size) {
087        resize(w, size.width, size.height);
088      }
089    
090      /**
091       * Moves the <code>{@link Window}</code> to the given location.
092       * @param w the target <code>Window</code>.
093       * @param where the location to move the <code>Window</code> to.
094       * @throws ActionFailedException if the <code>Window</code> is not enabled.
095       * @throws ActionFailedException if the <code>Window</code> is not movable by the user.
096       * @throws ActionFailedException if the <code>Window</code> is not showing on the screen.
097       */
098      public void moveTo(Window w, Point where) {
099        move(w, where.x, where.y);
100      }
101    
102      /**
103       * Closing the <code>{@link Window}</code>.
104       * @param w the target <code>Window</code>.
105       * @throws ActionFailedException if the <code>Window</code> is not enabled.
106       * @throws ActionFailedException if the <code>Window</code> is not showing on the screen.
107       */
108      @RunsInEDT
109      public void close(Window w) {
110        moveMouseIgnoringAnyError(w, closeInfo(w));
111        robot.close(w);
112      }
113    
114      @RunsInEDT
115      private static Point closeInfo(final Window w) {
116        return execute(new GuiQuery<Point>() {
117          protected Point executeInEDT() {
118            validateIsEnabledAndShowing(w);
119            return closeLocationOf(w);
120          }
121        });
122      }
123    
124      /**
125       * Shows the <code>{@link Window}</code>.
126       * @param w the target <code>Window</code>.
127       */
128      @RunsInEDT
129      public void show(Window w) {
130        robot.showWindow(w);
131      }
132    
133      /**
134       * Shows the <code>{@link Window}</code>, resized to the given size.
135       * @param w the target <code>Window</code>.
136       * @param size the size to resize the <code>Window</code> to.
137       */
138      @RunsInEDT
139      public void show(Window w, Dimension size) {
140        robot.showWindow(w, size);
141      }
142    
143      /**
144       * If the given <code>{@link Window}</code> is visible, brings it to the front and may make it the focused one.
145       * @param w the target <code>Window</code>.
146       */
147      @RunsInEDT
148      public void moveToFront(Window w) {
149        doMoveToFront(w);
150        robot.waitForIdle();
151      }
152    
153      @RunsInEDT
154      private static void doMoveToFront(final Window w) {
155        execute(new GuiTask() {
156          protected void executeInEDT() {
157            w.toFront();
158          }
159        });
160      }
161    
162      /**
163       * If the given <code>{@link Window}</code> is visible, sends it to the back and may cause it to lose focus or
164       * activation if it is the focused or active.
165       * @param w the target <code>Window</code>.
166       */
167      @RunsInEDT
168      public void moveToBack(Window w) {
169        doMoveToBack(w);
170        robot.waitForIdle();
171      }
172    
173      @RunsInEDT
174      private static void doMoveToBack(final Window w) {
175        execute(new GuiTask() {
176          protected void executeInEDT() {
177            w.toBack();
178          }
179        });
180      }
181    }