|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| JLabelFixture | Line # 38 | 51 | 0% | 28 | 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 java.awt.Point; | |
| 19 | import java.util.regex.Pattern; | |
| 20 | ||
| 21 | import javax.swing.JLabel; | |
| 22 | ||
| 23 | import org.fest.swing.core.*; | |
| 24 | import org.fest.swing.driver.JLabelDriver; | |
| 25 | import org.fest.swing.exception.ComponentLookupException; | |
| 26 | import org.fest.swing.timing.Timeout; | |
| 27 | ||
| 28 | /** | |
| 29 | * Understands functional testing of <code>{@link JLabel}</code>s: | |
| 30 | * <ul> | |
| 31 | * <li>user input simulation</li> | |
| 32 | * <li>state verification</li> | |
| 33 | * <li>property value query</li> | |
| 34 | * </ul> | |
| 35 | * | |
| 36 | * @author Alex Ruiz | |
| 37 | */ | |
| 38 | public class JLabelFixture extends ComponentFixture<JLabel> implements CommonComponentFixture, JComponentFixture, | |
| 39 | JPopupMenuInvokerFixture, TextDisplayFixture { | |
| 40 | ||
| 41 | private JLabelDriver driver; | |
| 42 | ||
| 43 | /** | |
| 44 | * Creates a new <code>{@link JLabelFixture}</code>. | |
| 45 | * @param robot performs simulation of user events on the given <code>JLabel</code>. | |
| 46 | * @param target the <code>JLabel</code> to be managed by this fixture. | |
| 47 | * @throws NullPointerException if <code>robot</code> is <code>null</code>. | |
| 48 | * @throws NullPointerException if <code>target</code> is <code>null</code>. | |
| 49 | */ | |
| 50 | 29 |
public JLabelFixture(Robot robot, JLabel target) { |
| 51 | 29 | super(robot, target); |
| 52 | 29 | createDriver(); |
| 53 | } | |
| 54 | ||
| 55 | /** | |
| 56 | * Creates a new <code>{@link JLabelFixture}</code>. | |
| 57 | * @param robot performs simulation of user events on a <code>JLabel</code>. | |
| 58 | * @param labelName the name of the <code>JLabel</code> to find using the given <code>Robot</code>. | |
| 59 | * @throws NullPointerException if <code>robot</code> is <code>null</code>. | |
| 60 | * @throws ComponentLookupException if a matching <code>JLabel</code> could not be found. | |
| 61 | * @throws ComponentLookupException if more than one matching <code>JLabel</code> is found. | |
| 62 | */ | |
| 63 | 1 |
public JLabelFixture(Robot robot, String labelName) { |
| 64 | 1 | super(robot, labelName, JLabel.class); |
| 65 | 1 | createDriver(); |
| 66 | } | |
| 67 | ||
| 68 | 30 |
private void createDriver() { |
| 69 | 30 | driver(new JLabelDriver(robot)); |
| 70 | } | |
| 71 | ||
| 72 | /** | |
| 73 | * Sets the <code>{@link JLabelDriver}</code> to be used by this fixture. | |
| 74 | * @param newDriver the new <code>JLabelDriver</code>. | |
| 75 | * @throws NullPointerException if the given driver is <code>null</code>. | |
| 76 | */ | |
| 77 | 55 |
protected final void driver(JLabelDriver newDriver) { |
| 78 | 55 | validateNotNull(newDriver); |
| 79 | 54 | driver = newDriver; |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * Returns the text of this fixture's <code>{@link JLabel}</code>. | |
| 84 | * @return the text of this fixture's <code>JLabel</code>. | |
| 85 | */ | |
| 86 | 1 |
public String text() { |
| 87 | 1 | return driver.textOf(target); |
| 88 | } | |
| 89 | ||
| 90 | /** | |
| 91 | * Simulates a user clicking this fixture's <code>{@link JLabel}</code>. | |
| 92 | * @return this fixture. | |
| 93 | * @throws IllegalStateException if this fixture's <code>JLabel</code> is disabled. | |
| 94 | * @throws IllegalStateException if this fixture's <code>JLabel</code> is not showing on the screen. | |
| 95 | */ | |
| 96 | 1 |
public JLabelFixture click() { |
| 97 | 1 | driver.click(target); |
| 98 | 1 | return this; |
| 99 | } | |
| 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 | 1 |
public JLabelFixture click(MouseButton button) { |
| 110 | 1 | driver.click(target, button); |
| 111 | 1 | 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 | 1 |
public JLabelFixture click(MouseClickInfo mouseClickInfo) { |
| 123 | 1 | driver.click(target, mouseClickInfo); |
| 124 | 1 | 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 | 1 |
public JLabelFixture doubleClick() { |
| 134 | 1 | driver.doubleClick(target); |
| 135 | 1 | 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 | 1 |
public JLabelFixture rightClick() { |
| 145 | 1 | driver.rightClick(target); |
| 146 | 1 | 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 | 1 |
public JLabelFixture focus() { |
| 156 | 1 | driver.focus(target); |
| 157 | 1 | 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 | 1 |
public JLabelFixture pressAndReleaseKey(KeyPressInfo keyPressInfo) { |
| 172 | 1 | driver.pressAndReleaseKey(target, keyPressInfo); |
| 173 | 1 | 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 | 1 |
public JLabelFixture pressAndReleaseKeys(int... keyCodes) { |
| 187 | 1 | driver.pressAndReleaseKeys(target, keyCodes); |
| 188 | 1 | 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 | 1 |
public JLabelFixture pressKey(int keyCode) { |
| 201 | 1 | driver.pressKey(target, keyCode); |
| 202 | 1 | 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 | 1 |
public JLabelFixture releaseKey(int keyCode) { |
| 215 | 1 | driver.releaseKey(target, keyCode); |
| 216 | 1 | 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 | 2 |
public JLabelFixture requireText(String expected) { |
| 226 | 2 | driver.requireText(target, expected); |
| 227 | 2 | 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 | 1 |
public JLabelFixture requireText(Pattern pattern) { |
| 240 | 1 | driver.requireText(target, pattern); |
| 241 | 1 | 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 | 1 |
public JLabelFixture requireToolTip(String expected) { |
| 252 | 1 | driver.requireToolTip(target, expected); |
| 253 | 1 | 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 | 1 |
public JLabelFixture requireToolTip(Pattern pattern) { |
| 267 | 1 | driver.requireToolTip(target, pattern); |
| 268 | 1 | 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 | 1 |
public JLabelFixture requireFocused() { |
| 277 | 1 | driver.requireFocused(target); |
| 278 | 1 | 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 | 1 |
public JLabelFixture requireEnabled() { |
| 287 | 1 | driver.requireEnabled(target); |
| 288 | 1 | 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 | 1 |
public JLabelFixture requireEnabled(Timeout timeout) { |
| 298 | 1 | driver.requireEnabled(target, timeout); |
| 299 | 1 | 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 | 1 |
public JLabelFixture requireDisabled() { |
| 308 | 1 | driver.requireDisabled(target); |
| 309 | 1 | 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 | 1 |
public JLabelFixture requireVisible() { |
| 318 | 1 | driver.requireVisible(target); |
| 319 | 1 | 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 | 1 |
public JLabelFixture requireNotVisible() { |
| 328 | 1 | driver.requireNotVisible(target); |
| 329 | 1 | 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 | 1 |
public Object clientProperty(Object key) { |
| 341 | 1 | 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 | 1 |
public JPopupMenuFixture showPopupMenu() { |
| 352 | 1 | 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 | 1 |
public JPopupMenuFixture showPopupMenuAt(Point p) { |
| 365 | 1 | return new JPopupMenuFixture(robot, driver.invokePopupMenu(target, p)); |
| 366 | } | |
| 367 | } | |
|
||||||||||||