001    /*
002     * Created on Jul 5, 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.BorderLayout;
019    import java.awt.Point;
020    import java.util.regex.Pattern;
021    
022    import javax.swing.JToolBar;
023    
024    import org.fest.swing.core.*;
025    import org.fest.swing.driver.JToolBarDriver;
026    import org.fest.swing.exception.*;
027    import org.fest.swing.timing.Timeout;
028    
029    /**
030     * Understands functional testing of <code>{@link JToolBar}</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 JToolBarFixture extends ContainerFixture<JToolBar> implements CommonComponentFixture,
040      JComponentFixture, JPopupMenuInvokerFixture {
041    
042      /**
043       * Understands constraints used to unfloat a floating <code>{@link JToolBar}</code>.
044       *
045       * @author Alex Ruiz
046       */
047      public enum UnfloatConstraint {
048        NORTH(BorderLayout.NORTH), EAST(BorderLayout.EAST), SOUTH(BorderLayout.SOUTH), WEST(BorderLayout.WEST);
049    
050        public final String value;
051    
052        UnfloatConstraint(String value) {
053          this.value = value;
054        }
055      }
056    
057      private JToolBarDriver driver;
058    
059      /**
060       * Creates a new <code>{@link JToolBarFixture}</code>.
061       * @param robot performs simulation of user events on the given <code>JToolBar</code>.
062       * @param target the <code>JToolBar</code> to be managed by this fixture.
063       * @throws NullPointerException if <code>robot</code> is <code>null</code>.
064       * @throws NullPointerException if <code>target</code> is <code>null</code>.
065       */
066      public JToolBarFixture(Robot robot, JToolBar target) {
067        super(robot, target);
068        createDriver();
069      }
070    
071      /**
072       * Creates a new <code>{@link JToolBarFixture}</code>.
073       * @param robot performs simulation of user events on a <code>JToolBar</code>.
074       * @param toolbarName the name of the <code>JToolBar</code> to find using the given <code>Robot</code>.
075       * @throws NullPointerException if <code>robot</code> is <code>null</code>.
076       * @throws ComponentLookupException if a matching <code>JToolBar</code> could not be found.
077       * @throws ComponentLookupException if more than one matching <code>JToolBar</code> is found.
078       */
079      public JToolBarFixture(Robot robot, String toolbarName) {
080        super(robot, toolbarName, JToolBar.class);
081        createDriver();
082      }
083    
084      private void createDriver() {
085        driver(new JToolBarDriver(robot));
086      }
087    
088      /**
089       * Sets the <code>{@link JToolBarDriver}</code> to be used by this fixture.
090       * @param newDriver the new <code>JToolBarDriver</code>.
091       * @throws NullPointerException if the given driver is <code>null</code>.
092       */
093      protected final void driver(JToolBarDriver newDriver) {
094        validateNotNull(newDriver);
095        driver = newDriver;
096      }
097    
098      /**
099       * Simulates a user dragging this fixture's <code>{@link JToolBar}</code> to the given location, causing it to float.
100       * @param point the point where the <code>JToolBar</code> will be floating to.
101       * @return this fixture.
102       * @throws ActionFailedException if the <code>JToolBar</code> is not floatable.
103       * @throws ActionFailedException if the <code>JToolBar</code> cannot be dragged.
104       */
105      public JToolBarFixture floatTo(Point point) {
106        driver.floatTo(target, point.x, point.y);
107        return this;
108      }
109    
110      /**
111       * Simulates a user unfloating this fixture's <code>{@link JToolBar}</code>.
112       * @return this fixture.
113       * @throws ActionFailedException if the dock container cannot be found.
114       */
115      public JToolBarFixture unfloat() {
116        driver.unfloat(target);
117        return this;
118      }
119    
120      /**
121       * Simulates a user dropping this fixture's  {@link JToolBar} to the requested constraint position.
122       * @param constraint the constraint position.
123       * @return this fixture.
124       * @throws ActionFailedException if the dock container cannot be found.
125       */
126      public JToolBarFixture unfloat(UnfloatConstraint constraint) {
127        driver.unfloat(target, constraint.value);
128        return this;
129      }
130    
131      /**
132       * Simulates a user clicking this fixture's <code>{@link JToolBar}</code>.
133       * @return this fixture.
134       */
135      public JToolBarFixture click() {
136        driver.click(target);
137        return this;
138      }
139    
140      /**
141       * Simulates a user clicking this fixture's <code>{@link JToolBar}</code>.
142       * @param button the button to click.
143       * @return this fixture.
144       */
145      public JToolBarFixture click(MouseButton button) {
146        driver.click(target, button);
147        return this;
148      }
149    
150      /**
151       * Simulates a user clicking this fixture's <code>{@link JToolBar}</code>.
152       * @param mouseClickInfo specifies the button to click and the times the button should be clicked.
153       * @return this fixture.
154       * @throws NullPointerException if the given <code>MouseClickInfo</code> is <code>null</code>.
155       */
156      public JToolBarFixture click(MouseClickInfo mouseClickInfo) {
157        driver.click(target, mouseClickInfo);
158        return this;
159      }
160    
161      /**
162       * Simulates a user double-clicking this fixture's <code>{@link JToolBar}</code>.
163       * @return this fixture.
164       */
165      public JToolBarFixture doubleClick() {
166        driver.doubleClick(target);
167        return this;
168      }
169    
170      /**
171       * Simulates a user right-clicking this fixture's <code>{@link JToolBar}</code>.
172       * @return this fixture.
173       */
174      public JToolBarFixture rightClick() {
175        driver.rightClick(target);
176        return this;
177      }
178    
179      /**
180       * Gives input focus to this fixture's <code>{@link JToolBar}</code>.
181       * @return this fixture.
182       */
183      public JToolBarFixture focus() {
184        driver.focus(target);
185        return this;
186      }
187    
188      /**
189       * Simulates a user pressing given key with the given modifiers on this fixture's <code>{@link JToolBar}</code>.
190       * Modifiers is a mask from the available <code>{@link java.awt.event.InputEvent}</code> masks.
191       * @param keyPressInfo specifies the key and modifiers to press.
192       * @return this fixture.
193       * @throws NullPointerException if the given <code>KeyPressInfo</code> is <code>null</code>.
194       * @throws IllegalArgumentException if the given code is not a valid key code.
195       * @see KeyPressInfo
196       */
197      public JToolBarFixture pressAndReleaseKey(KeyPressInfo keyPressInfo) {
198        driver.pressAndReleaseKey(target, keyPressInfo);
199        return this;
200      }
201    
202      /**
203       * Simulates a user pressing and releasing the given keys in this fixture's <code>{@link JToolBar}</code>. This method
204       * does not affect the current focus.
205       * @param keyCodes the codes of the keys to press.
206       * @return this fixture.
207       * @throws NullPointerException if the given array of codes is <code>null</code>.
208       * @throws IllegalArgumentException if any of the given code is not a valid key code.
209       * @see java.awt.event.KeyEvent
210       */
211      public JToolBarFixture pressAndReleaseKeys(int...keyCodes) {
212        driver.pressAndReleaseKeys(target, keyCodes);
213        return this;
214      }
215    
216      /**
217       * Simulates a user pressing the given key on this fixture's <code>{@link JToolBar}</code>.
218       * @param keyCode the code of the key to press.
219       * @return this fixture.
220       * @throws IllegalArgumentException if any of the given code is not a valid key code.
221       * @see java.awt.event.KeyEvent
222       */
223      public JToolBarFixture pressKey(int keyCode) {
224        driver.pressKey(target, keyCode);
225        return this;
226      }
227    
228      /**
229       * Simulates a user releasing the given key on this fixture's <code>{@link JToolBar}</code>.
230       * @param keyCode the code of the key to release.
231       * @return this fixture.
232       * @throws IllegalArgumentException if any of the given code is not a valid key code.
233       * @see java.awt.event.KeyEvent
234       */
235      public JToolBarFixture releaseKey(int keyCode) {
236        driver.releaseKey(target, keyCode);
237        return this;
238      }
239    
240      /**
241       * Asserts that this fixture's <code>{@link JToolBar}</code> has input focus.
242       * @return this fixture.
243       * @throws AssertionError if this fixture's <code>JToolBar</code> does not have input focus.
244       */
245      public JToolBarFixture requireFocused() {
246        driver.requireFocused(target);
247        return this;
248      }
249    
250      /**
251       * Asserts that this fixture's <code>{@link JToolBar}</code> is enabled.
252       * @return this fixture.
253       * @throws AssertionError if this fixture's <code>JToolBar</code> is disabled.
254       */
255      public JToolBarFixture requireEnabled() {
256        driver.requireEnabled(target);
257        return this;
258      }
259    
260      /**
261       * Asserts that this fixture's <code>{@link JToolBar}</code> is enabled.
262       * @param timeout the time this fixture will wait for the component to be enabled.
263       * @return this fixture.
264       * @throws WaitTimedOutError if this fixture's <code>JToolBar</code> is never enabled.
265       */
266      public JToolBarFixture requireEnabled(Timeout timeout) {
267        driver.requireEnabled(target, timeout);
268        return this;
269      }
270    
271      /**
272       * Asserts that this fixture's <code>{@link JToolBar}</code> is disabled.
273       * @return this fixture.
274       * @throws AssertionError if this fixture's <code>JToolBar</code> is enabled.
275       */
276      public JToolBarFixture requireDisabled() {
277        driver.requireDisabled(target);
278        return this;
279      }
280    
281      /**
282       * Asserts that this fixture's <code>{@link JToolBar}</code> is visible.
283       * @return this fixture.
284       * @throws AssertionError if this fixture's <code>JToolBar</code> is not visible.
285       */
286      public JToolBarFixture requireVisible() {
287        driver.requireVisible(target);
288        return this;
289      }
290    
291      /**
292       * Asserts that this fixture's <code>{@link JToolBar}</code> is not visible.
293       * @return this fixture.
294       * @throws AssertionError if this fixture's <code>JToolBar</code> is visible.
295       */
296      public JToolBarFixture requireNotVisible() {
297        driver.requireNotVisible(target);
298        return this;
299      }
300    
301    
302      /**
303       * Asserts that the toolTip in this fixture's <code>{@link JToolBar}</code> matches the given value.
304       * @param expected the given value. It can be a regular expression.
305       * @return this fixture.
306       * @throws AssertionError if the toolTip in this fixture's <code>JToolBar</code> does not match the given value.
307       * @since 1.2
308       */
309      public JToolBarFixture requireToolTip(String expected) {
310        driver.requireToolTip(target, expected);
311        return this;
312      }
313    
314      /**
315       * Asserts that the toolTip in this fixture's <code>{@link JToolBar}</code> matches the given regular expression
316       * pattern.
317       * @param pattern the regular expression pattern to match.
318       * @return this fixture.
319       * @throws NullPointerException if the given regular expression pattern is <code>null</code>.
320       * @throws AssertionError if the toolTip in this fixture's <code>JToolBar</code> does not match the given regular
321       * expression.
322       * @since 1.2
323       */
324      public JToolBarFixture requireToolTip(Pattern pattern) {
325        driver.requireToolTip(target, pattern);
326        return this;
327      }
328    
329      /**
330       * Returns the client property stored in this fixture's <code>{@link JToolBarFixture}</code>, under the given key.
331       * @param key the key to use to retrieve the client property.
332       * @return the value of the client property stored under the given key, or <code>null</code> if the property was
333       * not found.
334       * @throws NullPointerException if the given key is <code>null</code>.
335       * @since 1.2
336       */
337      public Object clientProperty(Object key) {
338        return driver.clientProperty(target, key);
339      }
340    
341      /**
342       * Shows a pop-up menu using this fixture's <code>{@link JToolBar}</code> as the invoker of the pop-up menu.
343       * @return a fixture that manages the displayed pop-up menu.
344       * @throws IllegalStateException if this fixture's <code>JToolBar</code> is disabled.
345       * @throws IllegalStateException if this fixture's <code>JToolBar</code> is not showing on the screen.
346       * @throws ComponentLookupException if a pop-up menu cannot be found.
347       */
348      public JPopupMenuFixture showPopupMenu() {
349        return new JPopupMenuFixture(robot, driver.invokePopupMenu(target));
350      }
351    
352      /**
353       * Shows a pop-up menu at the given point using this fixture's <code>{@link JToolBar}</code> as the invoker of the
354       * pop-up menu.
355       * @param p the given point where to show the pop-up menu.
356       * @return a fixture that manages the displayed pop-up menu.
357       * @throws IllegalStateException if this fixture's <code>JToolBar</code> is disabled.
358       * @throws IllegalStateException if this fixture's <code>JToolBar</code> is not showing on the screen.
359       * @throws ComponentLookupException if a pop-up menu cannot be found.
360       */
361      public JPopupMenuFixture showPopupMenuAt(Point p) {
362        return new JPopupMenuFixture(robot, driver.invokePopupMenu(target, p));
363      }
364    }