001 /*
002 * Created on Dec 8, 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 static org.fest.swing.core.MouseButton.LEFT_BUTTON;
019 import static org.fest.swing.core.MouseButton.RIGHT_BUTTON;
020
021 import javax.swing.JList;
022
023 import org.fest.swing.cell.JListCellReader;
024 import org.fest.swing.core.MouseButton;
025 import org.fest.swing.core.MouseClickInfo;
026 import org.fest.swing.exception.ActionFailedException;
027 import org.fest.swing.exception.ComponentLookupException;
028
029 /**
030 * Understands functional testing of single rows in <code>{@link JList}</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 JListItemFixture implements ItemFixture {
041
042 final JListFixture list;
043 final int index;
044
045 /**
046 * Creates a new </code>{@link JListItemFixture}</code>.
047 * @param list manages the <code>JList</code> containing the list item to be managed by this fixture.
048 * @param index index of the list item to be managed by this fixture.
049 * @throws NullPointerException if <code>list</code> is <code>null</code>.
050 */
051 public JListItemFixture(JListFixture list, int index) {
052 if (list == null) throw new NullPointerException("The given JListFixture should not be null");
053 this.list = list;
054 this.index = index;
055 }
056
057 /**
058 * Simulates a user selecting this fixture's list item.
059 * @return this fixture.
060 * @throws IllegalStateException if this fixture's <code>JList</code> is disabled.
061 * @throws IllegalStateException if this fixture's <code>JList</code> is not showing on the screen.
062 * @throws IndexOutOfBoundsException if this item's index is negative or greater than the index of the last item in
063 * the <code>JList</code>.
064 */
065 public final JListItemFixture select() {
066 list.selectItem(index);
067 return this;
068 }
069
070 /**
071 * Simulates a user clicking this fixture's list item.
072 * @return this fixture.
073 * @throws IllegalStateException if this fixture's <code>JList</code> is disabled.
074 * @throws IllegalStateException if this fixture's <code>JList</code> is not showing on the screen.
075 * @throws IndexOutOfBoundsException if this item's index is negative or greater than the index of the last item in
076 * the <code>JList</code>.
077 */
078 public final JListItemFixture click() {
079 list.clickItem(index);
080 return this;
081 }
082
083 /**
084 * Simulates a user clicking this fixture's list item.
085 * @param button the button to click.
086 * @return this fixture.
087 * @throws NullPointerException if the given <code>MouseButton</code> is <code>null</code>.
088 * @throws IllegalStateException if this fixture's <code>JList</code> is disabled.
089 * @throws IllegalStateException if this fixture's <code>JList</code> is not showing on the screen.
090 * @throws IndexOutOfBoundsException if this item's index is negative or greater than the index of the last item in
091 * the <code>JList</code>.
092 */
093 public final JListItemFixture click(MouseButton button) {
094 list.clickItem(index, button, 1);
095 return this;
096 }
097
098 /**
099 * Simulates a user clicking this fixture's list item.
100 * @param mouseClickInfo specifies the button to click and the times the button should be clicked.
101 * @return this fixture.
102 * @throws NullPointerException if the given <code>MouseClickInfo</code> is <code>null</code>.
103 * @throws IllegalStateException if this fixture's <code>JList</code> is disabled.
104 * @throws IllegalStateException if this fixture's <code>JList</code> is not showing on the screen.
105 * @throws IndexOutOfBoundsException if this item's index is negative or greater than the index of the last item in
106 * the <code>JList</code>.
107 */
108 public final JListItemFixture click(MouseClickInfo mouseClickInfo) {
109 list.clickItem(index, mouseClickInfo.button(), mouseClickInfo.times());
110 return this;
111 }
112
113 /**
114 * Simulates a user double-clicking this fixture's list item.
115 * @return this fixture.
116 * @throws IllegalStateException if this fixture's <code>JList</code> is disabled.
117 * @throws IllegalStateException if this fixture's <code>JList</code> is not showing on the screen.
118 * @throws IndexOutOfBoundsException if this item's index is negative or greater than the index of the last item in
119 * the <code>JList</code>.
120 */
121 public final JListItemFixture doubleClick() {
122 list.clickItem(index, LEFT_BUTTON, 2);
123 return this;
124 }
125
126 /**
127 * Simulates a user right-clicking this fixture's list item.
128 * @return this fixture.
129 * @throws IllegalStateException if this fixture's <code>JList</code> is disabled.
130 * @throws IllegalStateException if this fixture's <code>JList</code> is not showing on the screen.
131 * @throws IndexOutOfBoundsException if this item's index is negative or greater than the index of the last item in
132 * the <code>JList</code>.
133 */
134 public final JListItemFixture rightClick() {
135 list.clickItem(index, RIGHT_BUTTON, 1);
136 return this;
137 }
138
139 /**
140 * Shows a pop-up menu using this fixture's list item as the invoker of the pop-up menu.
141 * @return a fixture that manages the displayed pop-up menu.
142 * @throws IllegalStateException if this fixture's <code>JList</code> is disabled.
143 * @throws IllegalStateException if this fixture's <code>JList</code> is not showing on the screen.
144 * @throws IndexOutOfBoundsException if this item's index is negative or greater than the index of the last item in
145 * the <code>JList</code>.
146 * @throws ComponentLookupException if a pop-up menu cannot be found.
147 */
148 public final JPopupMenuFixture showPopupMenu() {
149 return list.showPopupMenuAt(index);
150 }
151
152 /**
153 * Returns the <code>String</code> representation of the value of this fixture's list item, using the
154 * <code>{@link JListCellReader}</code> from the <code>{@link JListFixture}</code> that created this
155 * <code>{@link JListItemFixture}</code>.
156 * @return the <code>String</code> representation of the value of this fixture's list item.
157 * @throws IndexOutOfBoundsException if this item's index is negative or greater than the index of the last item in
158 * the <code>JList</code>.
159 * @see JListFixture#cellReader(JListCellReader)
160 */
161 public final String value() {
162 return list.valueAt(index);
163 }
164
165 /**
166 * Simulates a user dragging this fixture's list item.
167 * @return this fixture.
168 * @throws IllegalStateException if this fixture's <code>JList</code> is disabled.
169 * @throws IllegalStateException if this fixture's <code>JList</code> is not showing on the screen.
170 * @throws IndexOutOfBoundsException if this item's index is negative or greater than the index of the last item in
171 * the <code>JList</code>.
172 */
173 public final JListItemFixture drag() {
174 list.drag(index);
175 return this;
176 }
177
178 /**
179 * Simulates a user dropping into this fixture's list item.
180 * @return this fixture.
181 * @throws IllegalStateException if this fixture's <code>JList</code> is disabled.
182 * @throws IllegalStateException if this fixture's <code>JList</code> is not showing on the screen.
183 * @throws IndexOutOfBoundsException if this item's index is negative or greater than the index of the last item in
184 * the <code>JList</code>.
185 * @throws ActionFailedException if there is no drag action in effect.
186 */
187 public final JListItemFixture drop() {
188 list.drop(index);
189 return this;
190 }
191
192 /**
193 * Returns the index of this fixture's list item.
194 * @return the index of this fixture's list item.
195 */
196 public final int index() { return index; }
197 }