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