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