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
21   202   16   1.5
4   69   0.76   14
14     1.14  
1    
 
  ComponentFixture       Line # 48 21 0% 16 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Oct 20, 2006
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 @2006-2010 the original author or authors.
15    */
16    package org.fest.swing.fixture;
17   
18    import static org.fest.assertions.Assertions.assertThat;
19    import static org.fest.swing.driver.ComponentDriver.propertyName;
20    import static org.fest.swing.fixture.ComponentFixtureValidator.notNullRobot;
21    import static org.fest.swing.fixture.ComponentFixtureValidator.notNullTarget;
22    import static org.fest.swing.format.Formatting.format;
23    import static org.fest.swing.query.ComponentBackgroundQuery.backgroundOf;
24    import static org.fest.swing.query.ComponentFontQuery.fontOf;
25    import static org.fest.swing.query.ComponentForegroundQuery.foregroundOf;
26   
27    import java.awt.Component;
28   
29    import org.fest.swing.core.Robot;
30    import org.fest.swing.core.Settings;
31    import org.fest.swing.driver.ComponentDriver;
32    import org.fest.swing.edt.GuiActionRunner;
33    import org.fest.swing.exception.ComponentLookupException;
34   
35    /**
36    * Understands functional testing of <code>{@link Component}</code>s:
37    * <ul>
38    * <li>user input simulation</li>
39    * <li>state verification</li>
40    * <li>property value query</li>
41    * </ul>
42    *
43    * @param <T> the type of <code>Component</code> that this fixture can manage.
44    *
45    * @author Alex Ruiz
46    * @author Yvonne Wang
47    */
 
48    public abstract class ComponentFixture<T extends Component> {
49   
50    /** Name of the property "font". */
51    protected static final String FONT_PROPERTY = "font";
52   
53    /** Name of the property "background". */
54    protected static final String BACKGROUND_PROPERTY = "background";
55   
56    /** Name of the property "foreground". */
57    protected static final String FOREGROUND_PROPERTY = "foreground";
58   
59    /** Performs simulation of user events on <code>{@link #target}</code> */
60    public final Robot robot;
61   
62    /**
63    * This fixture's <code>{@link Component}</code>.
64    * <p>
65    * <strong>Note:</strong> Access to this GUI component <em>must</em> be executed in the event dispatch thread. To do
66    * so, please execute a <code>{@link org.fest.swing.edt.GuiQuery GuiQuery}</code> or
67    * <code>{@link org.fest.swing.edt.GuiTask GuiTask}</code> (depending on what you need to do,) inside a
68    * <code>{@link GuiActionRunner}</code>. To learn more about Swing threading, please read the
69    * <a href="http://java.sun.com/javase/6/docs/api/javax/swing/package-summary.html#threading" target="_blank">Swing Threading Policy</a>.
70    * </p>
71    */
72    public final T target;
73   
74    /**
75    * Creates a new <code>{@link ComponentFixture}</code>.
76    * @param robot performs simulation of user events on a <code>Component</code>.
77    * @param type the type of the <code>Component</code> to find using the given <code>RobotFixture</code>.
78    * @throws NullPointerException if <code>robot</code> is <code>null</code>.
79    * @throws NullPointerException if <code>type</code> is <code>null</code>.
80    * @throws ComponentLookupException if a matching component could not be found.
81    * @throws ComponentLookupException if more than one matching component is found.
82    */
 
83  4 toggle public ComponentFixture(Robot robot, Class<? extends T> type) {
84  4 this(robot, findTarget(robot, type));
85    }
86   
 
87  7 toggle private static <C extends Component> C findTarget(Robot robot, Class<? extends C> type) {
88  7 validate(robot, type);
89  5 return robot.finder().findByType(type, requireShowing(robot));
90    }
91   
92    /**
93    * Validates that the given <code>{@link ComponentDriver}</code> is not <code>null</code>.
94    * @param driver the {@code ComponentDriver} to validate.
95    * @throws NullPointerException if {@code driver} is <code>null</code>.
96    */
 
97  2042 toggle protected static void validateNotNull(ComponentDriver driver) {
98  28 if (driver == null) throw new NullPointerException("The driver should not be null");
99    }
100   
101    /**
102    * Creates a new <code>{@link ComponentFixture}</code>.
103    * @param robot performs simulation of user events on a <code>Component</code>.
104    * @param name the name of the <code>Component</code> to find using the given <code>RobotFixture</code>.
105    * @param type the type of the <code>Component</code> to find using the given <code>RobotFixture</code>.
106    * @throws NullPointerException if <code>robot</code> is <code>null</code>.
107    * @throws NullPointerException if <code>type</code> is <code>null</code>.
108    * @throws ComponentLookupException if a matching component could not be found.
109    * @throws ComponentLookupException if more than one matching component is found.
110    */
 
111  33 toggle public ComponentFixture(Robot robot, String name, Class<? extends T> type) {
112  33 this(robot, findTarget(robot, name, type));
113    }
114   
 
115  81 toggle private static <C extends Component> C findTarget(Robot robot, String name, Class<? extends C> type) {
116  81 validate(robot, type);
117  79 return robot.finder().findByName(name, type, requireShowing(robot));
118    }
119   
 
120  88 toggle private static void validate(Robot robot, Class<?> type) {
121  88 notNullRobot(robot);
122  2 if (type == null) throw new NullPointerException("The type of component to look for should not be null");
123    }
124   
125    /**
126    * Returns whether showing components are the only ones participating in a component lookup. The returned value is
127    * obtained from the <code>{@link Settings#componentLookupScope() component lookup scope}</code> stored in this
128    * fixture's <code>{@link Robot}</code>.
129    * @return <code>true</code> if only showing components can participate in a component lookup, <code>false</code>
130    * otherwise.
131    */
 
132  188 toggle protected boolean requireShowing() {
133  188 return requireShowing(robot);
134    }
135   
 
136  272 toggle private static boolean requireShowing(Robot robot) {
137  272 return robot.settings().componentLookupScope().requireShowing();
138    }
139   
140    /**
141    * Creates a new <code>{@link ComponentFixture}</code>.
142    * @param robot performs simulation of user events on the given <code>Component</code>.
143    * @param target the <code>Component</code> to be managed by this fixture.
144    * @throws NullPointerException if <code>robot</code> is <code>null</code>.
145    * @throws NullPointerException if <code>target</code> is <code>null</code>.
146    */
 
147  1400 toggle public ComponentFixture(Robot robot, T target) {
148  1400 this.robot = notNullRobot(robot);
149  1398 this.target = notNullTarget(target);
150    }
151   
152    /**
153    * Returns a fixture that verifies the font of this fixture's <code>{@link Component}</code>.
154    * @return a fixture that verifies the font of this fixture's <code>Component</code>.
155    */
 
156  1 toggle public final FontFixture font() {
157  1 return new FontFixture(fontOf(target), propertyName(target, FONT_PROPERTY));
158    }
159   
160    /**
161    * Returns a fixture that verifies the background color of this fixture's <code>{@link Component}</code>.
162    * @return a fixture that verifies the background color of this fixture's <code>Component</code>.
163    */
 
164  1 toggle public final ColorFixture background() {
165  1 return new ColorFixture(backgroundOf(target), propertyName(target, BACKGROUND_PROPERTY));
166    }
167   
168    /**
169    * Returns a fixture that verifies the foreground color of this fixture's <code>{@link Component}</code>.
170    * @return a fixture that verifies the foreground color of this fixture's <code>Component</code>.
171    */
 
172  1 toggle public final ColorFixture foreground() {
173  1 return new ColorFixture(foregroundOf(target), propertyName(target, FOREGROUND_PROPERTY));
174    }
175   
176    /**
177    * Returns this fixture's <code>{@link Component}</code> casted to the given sub-type.
178    * @param <C> enforces that the given type is a sub-type of the managed <code>Component</code>.
179    * @param type the type that the managed <code>Component</code> will be casted to.
180    * @return this fixture's <code>Component</code> casted to the given sub-type.
181    * @throws AssertionError if this fixture's <code>Component</code> is not an instance of the given type.
182    */
 
183  1 toggle public final <C extends T> C targetCastedTo(Class<C> type) {
184  1 assertThat(target).as(format(target)).isInstanceOf(type);
185  1 return type.cast(target);
186    }
187   
188    /**
189    * Returns the GUI component in this fixture (same as <code>{@link #target}</code>.)
190    * <p>
191    * <strong>Note:</strong> Access to the GUI component returned by this method <em>must</em> be executed in the event
192    * dispatch thread. To do so, please execute a <code>{@link org.fest.swing.edt.GuiQuery GuiQuery}</code> or
193    * <code>{@link org.fest.swing.edt.GuiTask GuiTask}</code> (depending on what you need to do,) inside a
194    * <code>{@link GuiActionRunner}</code>. To learn more about Swing threading, please read the
195    * <a href="http://java.sun.com/javase/6/docs/api/javax/swing/package-summary.html#threading" target="_blank">Swing Threading Policy</a>.
196    * </p>
197    * @return the GUI component in this fixture.
198    */
 
199  187 toggle public final T component() {
200  187 return target;
201    }
202    }