001 /* 002 * Created on Dec 25, 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.JScrollBar; 022 023 import org.fest.swing.core.*; 024 import org.fest.swing.driver.JScrollBarDriver; 025 import org.fest.swing.exception.ComponentLookupException; 026 import org.fest.swing.exception.WaitTimedOutError; 027 import org.fest.swing.timing.Timeout; 028 029 /** 030 * Understands functional testing of <code>{@link JScrollBar}</code>s: 031 * <ul> 032 * <li>user input simulation</li> 033 * <li>state verification</li> 034 * <li>property value query</li> 035 * </ul> 036 * 037 * @author Alex Ruiz 038 */ 039 public class JScrollBarFixture extends ComponentFixture<JScrollBar> implements CommonComponentFixture, 040 JComponentFixture, JPopupMenuInvokerFixture { 041 042 private JScrollBarDriver driver; 043 044 /** 045 * Creates a new <code>{@link JScrollBarFixture}</code>. 046 * @param robot performs simulation of user events on the given <code>JScrollBar</code>. 047 * @param target the <code>JScrollBar</code> to be managed by this fixture. 048 * @throws NullPointerException if <code>robot</code> is <code>null</code>. 049 * @throws NullPointerException if <code>target</code> is <code>null</code>. 050 */ 051 public JScrollBarFixture(Robot robot, JScrollBar target) { 052 super(robot, target); 053 createDriver(); 054 } 055 056 /** 057 * Creates a new <code>{@link JScrollBarFixture}</code>. 058 * @param robot performs simulation of user events on a <code>JScrollBar</code>. 059 * @param scrollBarName the name of the <code>JScrollBar</code> to find using the given <code>Robot</code>. 060 * @throws NullPointerException if <code>robot</code> is <code>null</code>. 061 * @throws ComponentLookupException if a matching <code>JScrollBar</code> could not be found. 062 * @throws ComponentLookupException if more than one matching <code>JScrollBar</code> is found. 063 */ 064 public JScrollBarFixture(Robot robot, String scrollBarName) { 065 super(robot, scrollBarName, JScrollBar.class); 066 createDriver(); 067 } 068 069 private void createDriver() { 070 driver(new JScrollBarDriver(robot)); 071 } 072 073 /** 074 * Sets the <code>{@link JScrollBarDriver}</code> to be used by this fixture. 075 * @param newDriver the new <code>JScrollBarDriver</code>. 076 * @throws NullPointerException if the given driver is <code>null</code>. 077 */ 078 protected final void driver(JScrollBarDriver newDriver) { 079 validateNotNull(newDriver); 080 driver = newDriver; 081 } 082 083 /** 084 * Simulates a user clicking this fixture's <code>{@link JScrollBar}</code>. 085 * @return this fixture. 086 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 087 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 088 */ 089 public JScrollBarFixture click() { 090 driver.click(target); 091 return this; 092 } 093 094 /** 095 * Simulates a user clicking this fixture's <code>{@link JScrollBar}</code>. 096 * @param button the button to click. 097 * @throws NullPointerException if the given <code>MouseButton</code> is <code>null</code>. 098 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 099 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 100 * @return this fixture. 101 */ 102 public JScrollBarFixture click(MouseButton button) { 103 driver.click(target, button); 104 return this; 105 } 106 107 /** 108 * Simulates a user clicking this fixture's <code>{@link JScrollBar}</code>. 109 * @param mouseClickInfo specifies the button to click and the times the button should be clicked. 110 * @return this fixture. 111 * @throws NullPointerException if the given <code>MouseClickInfo</code> is <code>null</code>. 112 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 113 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 114 */ 115 public JScrollBarFixture click(MouseClickInfo mouseClickInfo) { 116 driver.click(target, mouseClickInfo); 117 return this; 118 } 119 120 /** 121 * Simulates a user double-clicking this fixture's <code>{@link JScrollBar}</code>. 122 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 123 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 124 * @return this fixture. 125 */ 126 public JScrollBarFixture doubleClick() { 127 driver.doubleClick(target); 128 return this; 129 } 130 131 /** 132 * Simulates a user right-clicking this fixture's <code>{@link JScrollBar}</code>. 133 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 134 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 135 * @return this fixture. 136 */ 137 public JScrollBarFixture rightClick() { 138 driver.rightClick(target); 139 return this; 140 } 141 142 /** 143 * Gives input focus to this fixture's <code>{@link JScrollBar}</code>. 144 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 145 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 146 * @return this fixture. 147 */ 148 public JScrollBarFixture focus() { 149 driver.focus(target); 150 return this; 151 } 152 153 /** 154 * Simulates a user pressing given key with the given modifiers on this fixture's <code>{@link JScrollBar}</code>. 155 * Modifiers is a mask from the available <code>{@link java.awt.event.InputEvent}</code> masks. 156 * @param keyPressInfo specifies the key and modifiers to press. 157 * @return this fixture. 158 * @throws NullPointerException if the given <code>KeyPressInfo</code> is <code>null</code>. 159 * @throws IllegalArgumentException if the given code is not a valid key code. 160 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 161 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 162 * @see KeyPressInfo 163 */ 164 public JScrollBarFixture pressAndReleaseKey(KeyPressInfo keyPressInfo) { 165 driver.pressAndReleaseKey(target, keyPressInfo); 166 return this; 167 } 168 169 /** 170 * Simulates a user pressing and releasing the given keys on the <code>{@link JScrollBar}</code> managed by this 171 * fixture. 172 * @param keyCodes one or more codes of the keys to press. 173 * @return this fixture. 174 * @throws NullPointerException if the given array of codes is <code>null</code>. 175 * @throws IllegalArgumentException if any of the given code is not a valid key code. 176 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 177 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 178 * @see java.awt.event.KeyEvent 179 */ 180 public JScrollBarFixture pressAndReleaseKeys(int... keyCodes) { 181 driver.pressAndReleaseKeys(target, keyCodes); 182 return this; 183 } 184 185 /** 186 * Simulates a user pressing the given key on this fixture's <code>{@link JScrollBar}</code>. 187 * @param keyCode the code of the key to press. 188 * @return this fixture. 189 * @throws IllegalArgumentException if any of the given code is not a valid key code. 190 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 191 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 192 * @see java.awt.event.KeyEvent 193 */ 194 public JScrollBarFixture pressKey(int keyCode) { 195 driver.pressKey(target, keyCode); 196 return this; 197 } 198 199 /** 200 * Simulates a user releasing the given key on this fixture's <code>{@link JScrollBar}</code>. 201 * @param keyCode the code of the key to release. 202 * @return this fixture. 203 * @throws IllegalArgumentException if any of the given code is not a valid key code. 204 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 205 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 206 * @see java.awt.event.KeyEvent 207 */ 208 public JScrollBarFixture releaseKey(int keyCode) { 209 driver.releaseKey(target, keyCode); 210 return this; 211 } 212 213 /** 214 * Simulates a user scrolling down one block (usually a page.) 215 * @return this fixture. 216 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 217 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 218 */ 219 public JScrollBarFixture scrollBlockDown() { 220 driver.scrollBlockDown(target); 221 return this; 222 } 223 224 /** 225 * Simulates a user scrolling down one block (usually a page,) the given number of times. 226 * @param times the number of times to scroll down one block. 227 * @return this fixture. 228 * @throws IllegalArgumentException if <code>times</code> is less than or equal to zero. 229 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 230 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 231 */ 232 public JScrollBarFixture scrollBlockDown(int times) { 233 driver.scrollBlockDown(target, times); 234 return this; 235 } 236 237 /** 238 * Simulates a user scrolling up one block (usually a page.) 239 * @return this fixture. 240 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 241 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 242 */ 243 public JScrollBarFixture scrollBlockUp() { 244 driver.scrollBlockUp(target); 245 return this; 246 } 247 248 /** 249 * Simulates a user scrolling up one block (usually a page,) the given number of times. 250 * @param times the number of times to scroll up one block. 251 * @return this fixture. 252 * @throws IllegalArgumentException if <code>times</code> is less than or equal to zero. 253 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 254 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 255 */ 256 public JScrollBarFixture scrollBlockUp(int times) { 257 driver.scrollBlockUp(target, times); 258 return this; 259 } 260 261 /** 262 * Simulates a user scrolling to the given position. 263 * @param position the position to scroll to. 264 * @return this fixture. 265 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 266 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 267 * @throws IllegalArgumentException if the given position is not within the <code>JScrollBar</code> bounds. 268 */ 269 public JScrollBarFixture scrollTo(int position) { 270 driver.scrollTo(target, position); 271 return this; 272 } 273 274 /** 275 * Simulates a user scrolling to the maximum position of this fixture's <code>{@link JScrollBar}</code>. 276 * @return this fixture. 277 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 278 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 279 */ 280 public JScrollBarFixture scrollToMaximum() { 281 driver.scrollToMaximum(target); 282 return this; 283 } 284 285 /** 286 * Simulates a user scrolling to the minimum position of this fixture's <code>{@link JScrollBar}</code>. 287 * @return this fixture. 288 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 289 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 290 */ 291 public JScrollBarFixture scrollToMinimum() { 292 driver.scrollToMinimum(target); 293 return this; 294 } 295 296 /** 297 * Simulates a user scrolling down one unit (usually a line.) 298 * @return this fixture. 299 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 300 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 301 */ 302 public JScrollBarFixture scrollUnitDown() { 303 driver.scrollUnitDown(target); 304 return this; 305 } 306 307 /** 308 * Simulates a user scrolling down one unit (usually a line,) the given number of times. 309 * @param times the number of times to scroll down one unit. 310 * @return this fixture. 311 * @throws IllegalArgumentException if <code>times</code> is less than or equal to zero. 312 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 313 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 314 */ 315 public JScrollBarFixture scrollUnitDown(int times) { 316 driver.scrollUnitDown(target, times); 317 return this; 318 } 319 320 /** 321 * Simulates a user scrolling up one unit (usually a line.) 322 * @return this fixture. 323 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 324 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 325 */ 326 public JScrollBarFixture scrollUnitUp() { 327 driver.scrollUnitUp(target); 328 return this; 329 } 330 331 /** 332 * Simulates a user scrolling up one unit (usually a line,) the given number of times. 333 * @param times the number of times to scroll up one unit. 334 * @return this fixture. 335 * @throws IllegalArgumentException if <code>times</code> is less than or equal to zero. 336 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 337 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 338 */ 339 public JScrollBarFixture scrollUnitUp(int times) { 340 driver.scrollUnitUp(target, times); 341 return this; 342 } 343 344 /** 345 * Asserts that the value of this fixture's <code>{@link JScrollBar}</code> is equal to the given one. 346 * @param value the expected value. 347 * @return this fixture. 348 * @throws AssertionError if the value of this fixture's <code>JScrollBar</code> is not equal to the given one. 349 */ 350 public JScrollBarFixture requireValue(int value) { 351 driver.requireValue(target, value); 352 return this; 353 } 354 355 /** 356 * Asserts that this fixture's <code>{@link JScrollBar}</code> has input focus. 357 * @return this fixture. 358 * @throws AssertionError if this fixture's <code>JScrollBar</code> does not have input focus. 359 */ 360 public JScrollBarFixture requireFocused() { 361 driver.requireFocused(target); 362 return this; 363 } 364 365 /** 366 * Asserts that this fixture's <code>{@link JScrollBar}</code> is enabled. 367 * @return this fixture. 368 * @throws AssertionError if this fixture's <code>JScrollBar</code> is disabled. 369 */ 370 public JScrollBarFixture requireEnabled() { 371 driver.requireEnabled(target); 372 return this; 373 } 374 375 /** 376 * Asserts that this fixture's <code>{@link JScrollBar}</code> is enabled. 377 * @param timeout the time this fixture will wait for the component to be enabled. 378 * @return this fixture. 379 * @throws WaitTimedOutError if this fixture's <code>JScrollBar</code> is never enabled. 380 */ 381 public JScrollBarFixture requireEnabled(Timeout timeout) { 382 driver.requireEnabled(target, timeout); 383 return this; 384 } 385 386 /** 387 * Asserts that this fixture's <code>{@link JScrollBar}</code> is disabled. 388 * @return this fixture. 389 * @throws AssertionError if this fixture's <code>JScrollBar</code> is enabled. 390 */ 391 public JScrollBarFixture requireDisabled() { 392 driver.requireDisabled(target); 393 return this; 394 } 395 396 /** 397 * Asserts that this fixture's <code>{@link JScrollBar}</code> is visible. 398 * @return this fixture. 399 * @throws AssertionError if this fixture's <code>JScrollBar</code> is not visible. 400 */ 401 public JScrollBarFixture requireVisible() { 402 driver.requireVisible(target); 403 return this; 404 } 405 406 /** 407 * Asserts that this fixture's <code>{@link JScrollBar}</code> is not visible. 408 * @return this fixture. 409 * @throws AssertionError if this fixture's <code>JScrollBar</code> is visible. 410 */ 411 public JScrollBarFixture requireNotVisible() { 412 driver.requireNotVisible(target); 413 return this; 414 } 415 416 /** 417 * Shows a pop-up menu using this fixture's <code>{@link JScrollBar}</code> as the invoker of the pop-up menu. 418 * @return a fixture that manages the displayed pop-up menu. 419 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 420 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 421 * @throws ComponentLookupException if a pop-up menu cannot be found. 422 */ 423 public JPopupMenuFixture showPopupMenu() { 424 return new JPopupMenuFixture(robot, driver.invokePopupMenu(target)); 425 } 426 427 /** 428 * Shows a pop-up menu at the given point using this fixture's <code>{@link JScrollBar}</code> as the invoker of the 429 * pop-up menu. 430 * @param p the given point where to show the pop-up menu. 431 * @return a fixture that manages the displayed pop-up menu. 432 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is disabled. 433 * @throws IllegalStateException if this fixture's <code>JScrollBar</code> is not showing on the screen. 434 * @throws ComponentLookupException if a pop-up menu cannot be found. 435 */ 436 public JPopupMenuFixture showPopupMenuAt(Point p) { 437 return new JPopupMenuFixture(robot, driver.invokePopupMenu(target, p)); 438 } 439 440 /** 441 * Returns the client property stored in this fixture's <code>{@link JScrollBar}</code>, under the given key. 442 * @param key the key to use to retrieve the client property. 443 * @return the value of the client property stored under the given key, or <code>null</code> if the property was 444 * not found. 445 * @throws NullPointerException if the given key is <code>null</code>. 446 * @since 1.2 447 */ 448 public Object clientProperty(Object key) { 449 return driver.clientProperty(target, key); 450 } 451 452 /** 453 * Asserts that the toolTip in this fixture's <code>{@link JScrollBar}</code> matches the given value. 454 * @param expected the given value. It can be a regular expression. 455 * @return this fixture. 456 * @throws AssertionError if the toolTip in this fixture's <code>JScrollBar</code> does not match the given value. 457 * @since 1.2 458 */ 459 public JScrollBarFixture requireToolTip(String expected) { 460 driver.requireToolTip(target, expected); 461 return this; 462 } 463 464 /** 465 * Asserts that the toolTip in this fixture's <code>{@link JScrollBar}</code> matches the given regular expression 466 * pattern. 467 * @param pattern the regular expression pattern to match. 468 * @return this fixture. 469 * @throws NullPointerException if the given regular expression pattern is <code>null</code>. 470 * @throws AssertionError if the toolTip in this fixture's <code>JScrollBar</code> does not match the given regular 471 * expression. 472 * @since 1.2 473 */ 474 public JScrollBarFixture requireToolTip(Pattern pattern) { 475 driver.requireToolTip(target, pattern); 476 return this; 477 } 478 }