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