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