001    /*
002     * Created on Dec 19, 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.core;
017    
018    import static java.lang.Math.max;
019    import static java.lang.Math.min;
020    import static org.fest.swing.core.ComponentLookupScope.DEFAULT;
021    import static org.fest.swing.util.Platform.*;
022    
023    import org.fest.util.VisibleForTesting;
024    
025    /**
026     * Understands configuration settings.
027     *
028     * @author Alex Ruiz
029     */
030    public class Settings {
031    
032      private static final int DEFAULT_DELAY = 30000;
033    
034      private ComponentLookupScope componentLookupScope;
035      private int timeoutToBeVisible;
036      private int timeoutToFindPopup;
037      private int timeoutToFindSubMenu;
038      private int delayBetweenEvents;
039      private int dragDelay;
040      private int dropDelay;
041      private int eventPostingDelay;
042      private int idleTimeout;
043    
044      private java.awt.Robot robot;
045    
046      public Settings() {
047        timeoutToBeVisible(DEFAULT_DELAY);
048        timeoutToFindPopup(DEFAULT_DELAY);
049        timeoutToFindSubMenu(100);
050        delayBetweenEvents(60);
051        dragDelay(0);
052        dropDelay(0);
053        eventPostingDelay(100);
054        componentLookupScope(DEFAULT);
055        idleTimeout(10000);
056      }
057    
058      void attachTo(java.awt.Robot newRobot) {
059        robot = newRobot;
060        if (delayBetweenEvents < 0) delayBetweenEvents = this.robot.getAutoDelay();
061        else updateRobotAutoDelay();
062      }
063    
064      @VisibleForTesting
065      java.awt.Robot robot() { return robot; }
066    
067      /**
068       * Returns a value representing the millisecond count in between generated events. The default is 60 milliseconds.
069       * @return a value representing the millisecond count in between generated events.
070       */
071      public int delayBetweenEvents() {
072        return delayBetweenEvents;
073      }
074    
075      /**
076       * Updates the value representing the millisecond count in between generated events. Usually just set to 100-200 if
077       * you want to slow down the playback to simulate actual user input. The default is 60 milliseconds.
078       * <p>
079       * To change the speed of a GUI test, you need to change the values of both <code>delayBetweenEvents</code> and
080       * <code>eventPostingDelay</code>.
081       * </p>
082       * @param ms the millisecond count in between generated events. It should be between -1 and 60000.
083       * @see #eventPostingDelay(int)
084       */
085      public void delayBetweenEvents(int ms) {
086        delayBetweenEvents = valueToUpdate(ms, -1, 60000);
087        if (robot != null) updateRobotAutoDelay();
088      }
089    
090      private void updateRobotAutoDelay() {
091        robot.setAutoDelay(delayBetweenEvents);
092      }
093    
094      /**
095       * Returns the number of milliseconds to wait for a component to be visible. The default value is 30000 milliseconds.
096       * @return the number of milliseconds to wait for a component to be visible.
097       */
098      public int timeoutToBeVisible() {
099        return timeoutToBeVisible;
100      }
101    
102      /**
103       * Updates the number of milliseconds to wait for a component to be visible. The default value is 30000 milliseconds.
104       * @param ms the time in milliseconds. It should be between 0 and 60000.
105       */
106      public void timeoutToBeVisible(int ms) {
107        timeoutToBeVisible = valueToUpdate(ms, 0, 60000);
108      }
109    
110      /**
111       * Returns the number of milliseconds to wait before failing to find a pop-up menu that should appear. The default
112       * value is 30000 milliseconds.
113       * @return the number of milliseconds to wait before failing to find a pop-up menu that should appear.
114       */
115      public int timeoutToFindPopup() {
116        return timeoutToFindPopup;
117      }
118    
119      /**
120       * Updates the number of milliseconds to wait before failing to find a pop-up menu that should appear. The default
121       * value is 30000 milliseconds.
122       * @param ms the time in milliseconds. It should be between 0 and 60000.
123       */
124      public void timeoutToFindPopup(int ms) {
125        timeoutToFindPopup = valueToUpdate(ms, 0, 60000);
126      }
127    
128      /**
129       * Returns the number of milliseconds to wait for a sub-menu to appear. The default value is 100 milliseconds.
130       * @return  the number of milliseconds to wait for a sub-menu to appear.
131       * @since 1.2
132       */
133      public int timeoutToFindSubMenu() {
134        return timeoutToFindSubMenu;
135      }
136    
137      /**
138       * Updates the number of milliseconds to wait for a sub-menu to appear. The default value is 100 milliseconds.
139       * @param ms the time in milliseconds. It should be between 0 and 10000.
140       * @since 1.2
141       */
142      public void timeoutToFindSubMenu(int ms) {
143        timeoutToFindSubMenu = valueToUpdate(ms, 0, 10000);
144      }
145    
146      /**
147       * Returns the number of milliseconds to wait between a pressing a mouse button and moving the mouse. The default
148       * value for Mac OS X or the X11 Windowing system is 100 milliseconds. For other platforms, the default value is 0.
149       * @return the number of milliseconds to wait between a pressing a mouse button and moving the mouse.
150       */
151      public int dragDelay() {
152        return dragDelay;
153      }
154    
155      /**
156       * Updates the number of milliseconds to wait between a pressing a mouse button and moving the mouse. The default
157       * value for Mac OS X or the X11 Windowing system is 100 milliseconds. For other platforms, the default value is 0.
158       * @param ms the time in milliseconds. For Mac OS X or the X11 Windowing system, the minimum value is 100. For other
159       * platforms the minimum value is 0. The maximum value for all platforms is 60000.
160       */
161      public void dragDelay(int ms) {
162        int min = isX11() || isOSX() ? 100 : 0;
163        dragDelay = valueToUpdate(ms, min, 60000);
164      }
165    
166      /**
167       * Returns the number of milliseconds before checking for idle. The default value is 100 milliseconds.
168       * @return the number of milliseconds before checking for idle.
169       */
170      public int eventPostingDelay() {
171        return eventPostingDelay;
172      }
173    
174      /**
175       * Updates the number of milliseconds before checking for idle. This allows the system a little time to put a native
176       * event onto the AWT event queue. The default value is 100 milliseconds.
177       * <p>
178       * To change the speed of a GUI test, you need to change the values of both <code>delayBetweenEvents</code> and
179       * <code>eventPostingDelay</code>.
180       * </p>
181       * @param ms the time in milliseconds. It should be between 0 and 1000.
182       * @see #delayBetweenEvents(int)
183       */
184      public void eventPostingDelay(int ms) {
185        eventPostingDelay = valueToUpdate(ms, 0, 1000);
186      }
187    
188      /**
189       * Returns the number of milliseconds between the final mouse movement and mouse release to ensure drop ends. The
190       * default value for Windows is 200. For other platforms, the default value is 0.
191       * @return the number of milliseconds between the final mouse movement and mouse release to ensure drop ends.
192       */
193      public int dropDelay() {
194        return dropDelay;
195      }
196    
197      /**
198       * Updates the number of milliseconds between the final mouse movement and mouse release to ensure drop ends. The
199       * default value for Windows is 200. For other platforms, the default value is 0.
200       * @param ms the time in milliseconds. For Windows, the minimum value is 200. For other platforms, the minimum value
201       * is 0. The maximum value for all platforms is 60000.
202       */
203      public void dropDelay(int ms) {
204        int min = isWindows() ? 200 : 0;
205        dropDelay = valueToUpdate(ms, min, 60000);
206      }
207    
208      /**
209       * Returns the scope of component lookups. This setting only affects the component fixtures in the package
210       * <code>org.fest.swing.fixture</code>. The default value is <code>{@link ComponentLookupScope#DEFAULT}</code>.
211       * @return the scope of component lookups.
212       */
213      public ComponentLookupScope componentLookupScope() {
214        return componentLookupScope;
215      }
216    
217      /**
218       * Updates the scope of component lookups.  This setting only affects the component fixtures in the package
219       * <code>org.fest.swing.fixture</code>. The default value is <code>{@link ComponentLookupScope#DEFAULT}</code>.
220       * @param scope the new value for the scope.
221       */
222      public void componentLookupScope(ComponentLookupScope scope) {
223        componentLookupScope = scope;
224      }
225    
226      /**
227       * Returns the time (in milliseconds) to wait for an idle AWT event queue. The default value is 10000 milliseconds.
228       * @return the time (in milliseconds) to wait for an idle AWT event queue.
229       */
230      public int idleTimeout() {
231        return idleTimeout;
232      }
233    
234      /**
235       * Updates the time (in milliseconds) to wait for an idle AWT event queue.
236       * @param ms the new time. The value should be equal to or greater than zero.
237       */
238      public void idleTimeout(int ms) {
239        this.idleTimeout = valueToUpdate(ms, 0, Integer.MAX_VALUE);
240      }
241    
242      private int valueToUpdate(int value, int min, int max) {
243        return max(min, min(max, value));
244      }
245    }