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