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