Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
17   155   14   1.42
4   65   0.82   12
12     1.17  
1    
 
  AbstractButtonDriver       Line # 46 17 0% 14 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Feb 28, 2008
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10    * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11    * or implied. See the License for the specific language governing permissions and limitations under
12    * the License.
13    *
14    * Copyright @2008-2010 the original author or authors.
15    */
16    package org.fest.swing.driver;
17   
18    import static org.fest.assertions.Assertions.assertThat;
19    import static org.fest.swing.driver.AbstractButtonSelectedQuery.isSelected;
20    import static org.fest.swing.driver.ComponentStateValidator.validateIsEnabledAndShowing;
21    import static org.fest.swing.driver.TextAssert.verifyThat;
22    import static org.fest.swing.edt.GuiActionRunner.execute;
23   
24    import java.util.regex.Pattern;
25   
26    import javax.swing.AbstractButton;
27   
28    import org.fest.assertions.Description;
29    import org.fest.swing.annotation.RunsInEDT;
30    import org.fest.swing.core.Robot;
31    import org.fest.swing.edt.GuiQuery;
32   
33    /**
34    * Understands functional testing of <code>{@link AbstractButton}</code>s:
35    * <ul>
36    * <li>user input simulation</li>
37    * <li>state verification</li>
38    * <li>property value query</li>
39    * </ul>
40    * This class is intended for internal use only. Please use the classes in the package
41    * <code>{@link org.fest.swing.fixture}</code> in your tests.
42    *
43    * @author Alex Ruiz
44    * @author Yvonne Wang
45    */
 
46    public class AbstractButtonDriver extends JComponentDriver implements TextDisplayDriver<AbstractButton> {
47   
48    private static final String SELECTED_PROPERTY = "selected";
49    private static final String TEXT_PROPERTY = "text";
50   
51    /**
52    * Creates a new </code>{@link AbstractButtonDriver}</code>.
53    * @param robot the robot to use to simulate user input.
54    */
 
55  222 toggle public AbstractButtonDriver(Robot robot) {
56  222 super(robot);
57    }
58   
59    /**
60    * Asserts that the text in the given button is equal to or matches the specified <code>String</code>.
61    * @param button the given button.
62    * @param expected the text to match. It can be a regular expression.
63    * @throws AssertionError if the text of the button is not equal to or does not match the given one.
64    */
 
65  4 toggle @RunsInEDT
66    public void requireText(AbstractButton button, String expected) {
67  4 verifyThat(textOf(button)).as(propertyName(button, TEXT_PROPERTY)).isEqualOrMatches(expected);
68    }
69   
70    /**
71    * Asserts that the text in the given button matches the given regular expression pattern.
72    * @param button the given button.
73    * @param pattern the regular expression pattern to match.
74    * @throws NullPointerException if the given regular expression pattern is <code>null</code>.
75    * @throws AssertionError if the text of the button does not match the given regular expression pattern.
76    * @since 1.2
77    */
 
78  2 toggle public void requireText(AbstractButton button, Pattern pattern) {
79  2 verifyThat(textOf(button)).as(propertyName(button, TEXT_PROPERTY)).matches(pattern);
80    }
81   
82    /**
83    * Returns the text of the given button.
84    * @param button the given button.
85    * @return the text of the given button.
86    */
 
87  7 toggle @RunsInEDT
88    public String textOf(AbstractButton button) {
89  7 return AbstractButtonTextQuery.textOf(button);
90    }
91   
92    /**
93    * Selects the given button only it is not already selected.
94    * @param button the target button.
95    * @throws IllegalStateException if the button is disabled.
96    * @throws IllegalStateException if the button is not showing on the screen.
97    */
 
98  4 toggle @RunsInEDT
99    public void select(AbstractButton button) {
100  1 if (validateAndFindIsSelected(button)) return;
101  1 robot.click(button);
102    }
103   
104    /**
105    * Unselects the given button only if it is selected.
106    * @param button the target button.
107    * @throws IllegalStateException if the button is disabled.
108    * @throws IllegalStateException if the button is not showing on the screen.
109    */
 
110  4 toggle @RunsInEDT
111    public void unselect(AbstractButton button) {
112  1 if (!validateAndFindIsSelected(button)) return;
113  1 robot.click(button);
114    }
115   
 
116  8 toggle @RunsInEDT
117    private static boolean validateAndFindIsSelected(final AbstractButton button) {
118  8 return execute(new GuiQuery<Boolean>() {
 
119  8 toggle protected Boolean executeInEDT() {
120  8 validateIsEnabledAndShowing(button);
121  4 return button.isSelected();
122    }
123    });
124    }
125   
126    /**
127    * Verifies that the button is selected.
128    * @param button the given button.
129    * @throws AssertionError if the button is not selected.
130    */
 
131  2 toggle @RunsInEDT
132    public void requireSelected(AbstractButton button) {
133  2 assertThatButtonIsSelected(button, true);
134    }
135   
136    /**
137    * Verifies that the button is not selected.
138    * @param button the given button.
139    * @throws AssertionError if the button is selected.
140    */
 
141  2 toggle @RunsInEDT
142    public void requireNotSelected(AbstractButton button) {
143  2 assertThatButtonIsSelected(button, false);
144    }
145   
 
146  4 toggle @RunsInEDT
147    private void assertThatButtonIsSelected(AbstractButton button, boolean selected) {
148  4 assertThat(isSelected(button)).as(selectedProperty(button)).isEqualTo(selected);
149    }
150   
 
151  4 toggle @RunsInEDT
152    private static Description selectedProperty(AbstractButton button) {
153  4 return propertyName(button, SELECTED_PROPERTY);
154    }
155    }