001 /*
002 * Created on Oct 20, 2006
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 @2006-2010 the original author or authors.
015 */
016 package org.fest.swing.fixture;
017
018 import javax.swing.JMenuItem;
019
020 import org.fest.swing.core.KeyPressInfo;
021 import org.fest.swing.core.Robot;
022 import org.fest.swing.driver.JMenuItemDriver;
023 import org.fest.swing.exception.ActionFailedException;
024 import org.fest.swing.exception.ComponentLookupException;
025 import org.fest.swing.timing.Timeout;
026
027 /**
028 * Understands functional testing of <code>{@link JMenuItem}</code>s:
029 * <ul>
030 * <li>user input simulation</li>
031 * <li>state verification</li>
032 * <li>property value query</li>
033 * </ul>
034 *
035 * @author Alex Ruiz
036 */
037 public class JMenuItemFixture extends ComponentFixture<JMenuItem> implements KeyboardInputSimulationFixture,
038 StateVerificationFixture {
039
040 private JMenuItemDriver driver;
041
042 /**
043 * Creates a new <code>{@link JMenuItemFixture}</code>.
044 * @param robot performs simulation of user events on a <code>JMenuItem</code>.
045 * @param menuItemName the name of the <code>JMenuItem</code> to find using the given <code>Robot</code>.
046 * @throws NullPointerException if <code>robot</code> is <code>null</code>.
047 * @throws ComponentLookupException if a matching <code>JMenuItem</code> could not be found.
048 * @throws ComponentLookupException if more than one matching <code>JMenuItem</code> is found.
049 */
050 public JMenuItemFixture(Robot robot, String menuItemName) {
051 this(robot, robot.finder().findByName(menuItemName, JMenuItem.class, false));
052 }
053
054 /**
055 * Creates a new <code>{@link JMenuItemFixture}</code>.
056 * @param robot performs simulation of user events on the given <code>JMenuItem</code>.
057 * @param target the <code>JMenuItem</code> to be managed by this fixture.
058 * @throws NullPointerException if <code>robot</code> is <code>null</code>.
059 * @throws NullPointerException if <code>target</code> is <code>null</code>.
060 */
061 public JMenuItemFixture(Robot robot, JMenuItem target) {
062 super(robot, target);
063 driver(new JMenuItemDriver(robot));
064 }
065
066 /**
067 * Sets the <code>{@link JMenuItemDriver}</code> to be used by this fixture.
068 * @param newDriver the new <code>JMenuItemDriver</code>.
069 * @throws NullPointerException if the given driver is <code>null</code>.
070 */
071 protected final void driver(JMenuItemDriver newDriver) {
072 validateNotNull(newDriver);
073 driver = newDriver;
074 }
075
076 /**
077 * Simulates a user selecting this fixture's <code>{@link JMenuItem}</code>.
078 * @return this fixture.
079 * @throws IllegalStateException if this fixture's <code>JMenuItem</code> is disabled.
080 * @throws IllegalStateException if this fixture's <code>JMenuItem</code> is not showing on the screen.
081 * @throws ActionFailedException if the menu has a pop-up and it fails to show up.
082 */
083 public JMenuItemFixture click() {
084 driver.click(target);
085 return this;
086 }
087
088 /**
089 * Gives input focus to this fixture's <code>{@link JMenuItem}</code>.
090 * @throws IllegalStateException if this fixture's <code>JMenuItem</code> is disabled.
091 * @throws IllegalStateException if this fixture's <code>JMenuItem</code> is not showing on the screen.
092 * @return this fixture.
093 */
094 public JMenuItemFixture focus() {
095 driver.focus(target);
096 return this;
097 }
098
099 /**
100 * Simulates a user pressing given key with the given modifiers on this fixture's <code>{@link JMenuItem}</code>.
101 * Modifiers is a mask from the available <code>{@link java.awt.event.InputEvent}</code> masks.
102 * @param keyPressInfo specifies the key and modifiers to press.
103 * @return this fixture.
104 * @throws NullPointerException if the given <code>KeyPressInfo</code> is <code>null</code>.
105 * @throws IllegalArgumentException if the given code is not a valid key code.
106 * @throws IllegalStateException if this fixture's <code>JMenuItem</code> is disabled.
107 * @throws IllegalStateException if this fixture's <code>JMenuItem</code> is not showing on the screen.
108 * @see KeyPressInfo
109 */
110 public JMenuItemFixture pressAndReleaseKey(KeyPressInfo keyPressInfo) {
111 driver.pressAndReleaseKey(target, keyPressInfo);
112 return this;
113 }
114
115 /**
116 * Simulates a user pressing and releasing the given keys on this fixture's <code>{@link JMenuItem}</code>.
117 * @param keyCodes one or more codes of the keys to press.
118 * @return this fixture.
119 * @throws IllegalStateException if this fixture's <code>JMenuItem</code> is disabled.
120 * @throws IllegalStateException if this fixture's <code>JMenuItem</code> is not showing on the screen.
121 * @see java.awt.event.KeyEvent
122 */
123 public JMenuItemFixture pressAndReleaseKeys(int... keyCodes) {
124 driver.pressAndReleaseKeys(target, keyCodes);
125 return this;
126 }
127
128 /**
129 * Simulates a user pressing the given key on this fixture's <code>{@link JMenuItem}</code>.
130 * @param keyCode the code of the key to press.
131 * @return this fixture.
132 * @throws IllegalStateException if this fixture's <code>JMenuItem</code> is disabled.
133 * @throws IllegalStateException if this fixture's <code>JMenuItem</code> is not showing on the screen.
134 * @see java.awt.event.KeyEvent
135 */
136 public JMenuItemFixture pressKey(int keyCode) {
137 driver.pressKey(target, keyCode);
138 return this;
139 }
140
141 /**
142 * Simulates a user releasing the given key on this fixture's <code>{@link JMenuItem}</code>.
143 * @param keyCode the code of the key to release.
144 * @return this fixture.
145 * @throws IllegalStateException if this fixture's <code>JMenuItem</code> is disabled.
146 * @throws IllegalStateException if this fixture's <code>JMenuItem</code> is not showing on the screen.
147 * @see java.awt.event.KeyEvent
148 */
149 public JMenuItemFixture releaseKey(int keyCode) {
150 driver.releaseKey(target, keyCode);
151 return this;
152 }
153
154 /**
155 * Asserts that this fixture's <code>{@link JMenuItem}</code> is enabled.
156 * @return this fixture.
157 * @throws AssertionError if this fixture's <code>JMenuItem</code> is disabled.
158 */
159 public JMenuItemFixture requireEnabled() {
160 driver.requireEnabled(target);
161 return this;
162 }
163
164 /**
165 * Asserts that this fixture's <code>{@link JMenuItem}</code> is enabled.
166 * @param timeout the time this fixture will wait for the component to be enabled.
167 * @return this fixture.
168 * @throws org.fest.swing.exception.WaitTimedOutError if this fixture's <code>JMenuItem</code> is never enabled.
169 */
170 public JMenuItemFixture requireEnabled(Timeout timeout) {
171 driver.requireEnabled(target, timeout);
172 return this;
173 }
174
175 /**
176 * Asserts that this fixture's <code>{@link JMenuItem}</code> is disabled.
177 * @return this fixture.
178 * @throws AssertionError if this fixture's <code>JMenuItem</code> is enabled.
179 */
180 public JMenuItemFixture requireDisabled() {
181 driver.requireDisabled(target);
182 return this;
183 }
184
185 /**
186 * Asserts that this fixture's <code>{@link JMenuItem}</code> is visible.
187 * @return this fixture.
188 * @throws AssertionError if this fixture's <code>JMenuItem</code> is not visible.
189 */
190 public JMenuItemFixture requireVisible() {
191 driver.requireVisible(target);
192 return this;
193 }
194
195 /**
196 * Asserts that this fixture's <code>{@link JMenuItem}</code> is not visible.
197 * @return this fixture.
198 * @throws AssertionError if this fixture's <code>JMenuItem</code> is visible.
199 */
200 public JMenuItemFixture requireNotVisible() {
201 driver.requireNotVisible(target);
202 return this;
203 }
204 }