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