Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
52   423   34   1.62
4   120   0.65   32
32     1.06  
1    
 
  JTableCellFixture       Line # 56 52 0% 34 0 100% 1.0
 
No Tests
 
1    /*
2    * Created on Sep 10, 2007
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10    * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11    * or implied. See the License for the specific language governing permissions and limitations under
12    * the License.
13    *
14    * Copyright @2007-2010 the original author or authors.
15    */
16    package org.fest.swing.fixture;
17   
18    import static org.fest.swing.core.MouseButton.LEFT_BUTTON;
19    import static org.fest.swing.core.MouseButton.RIGHT_BUTTON;
20   
21    import java.awt.Component;
22    import java.util.regex.Pattern;
23   
24    import javax.swing.JTable;
25   
26    import org.fest.swing.cell.JTableCellReader;
27    import org.fest.swing.cell.JTableCellWriter;
28    import org.fest.swing.core.MouseButton;
29    import org.fest.swing.core.MouseClickInfo;
30    import org.fest.swing.data.TableCell;
31    import org.fest.swing.driver.JTableDriver;
32    import org.fest.swing.exception.ActionFailedException;
33    import org.fest.swing.exception.ComponentLookupException;
34   
35    /**
36    * Understands functional testing of single cells in <code>{@link JTable}</code>s:
37    * <ul>
38    * <li>user input simulation</li>
39    * <li>state verification</li>
40    * <li>property value query</li>
41    * </ul>
42    * <p>
43    * Example:
44    * <pre>
45    * // import static org.fest.swing.data.TableCell.row;
46    * {@link JTableCellFixture} cell = dialog.{@link JTableFixture table}("records").cell({@link TableCell#row(int) row}(3).column(0));
47    * cell.select().showPopupMenu();
48    * </pre>
49    * </p>
50    *
51    * @author Alex Ruiz
52    * @author Yvonne Wang
53    *
54    * @see TableCell
55    */
 
56    public class JTableCellFixture implements ItemFixture {
57   
58    private final JTableFixture table;
59    private final TableCell cell;
60   
61    /**
62    * Creates a new <code>{@link JTableCellFixture}</code>.
63    * @param table handles the <code>JTable</code> containing the cell in this fixture.
64    * @param cell row and column indices of the table cell to be managed by this fixture.
65    * @throws NullPointerException if <code>table</code> is <code>null</code>.
66    * @throws NullPointerException if <code>cell</code> is <code>null</code>.
67    */
 
68  41 toggle protected JTableCellFixture(JTableFixture table, TableCell cell) {
69  41 validateNotNull(table);
70  40 validateNotNull(cell);
71  39 this.table = table;
72  39 this.cell = cell;
73    }
74   
 
75  41 toggle private void validateNotNull(JTableFixture newTable) {
76  1 if (newTable == null) throw new NullPointerException("The JTableFixture should not be null");
77    }
78   
 
79  40 toggle private void validateNotNull(TableCell newCell) {
80  1 if (newCell == null) throw new NullPointerException("The TableCell should not be null");
81    }
82   
 
83  1 toggle JTableFixture table() { return table; }
 
84  1 toggle TableCell cell() { return cell; }
85   
86    /**
87    * Simulates a user selecting this fixture's table cell.
88    * @return this fixture.
89    * @throws IllegalStateException if this fixture's <code>JTable</code> is disabled.
90    * @throws IllegalStateException if this fixture's <code>JTable</code> is not showing on the screen.
91    */
 
92  1 toggle public JTableCellFixture select() {
93  1 table.selectCell(cell);
94  1 return this;
95    }
96   
97    /**
98    * Simulates a user clicking this fixture's table cell.
99    * @return this fixture.
100    * @throws IllegalStateException if this fixture's <code>JTable</code> is disabled.
101    * @throws IllegalStateException if this fixture's <code>JTable</code> is not showing on the screen.
102    */
 
103  6 toggle public JTableCellFixture click() {
104  6 table.click(cell, LEFT_BUTTON);
105  6 return this;
106    }
107   
108    /**
109    * Simulates a user clicking this fixture's table cell.
110    * @param mouseClickInfo specifies the button to click and the times the button should be clicked.
111    * @return this fixture.
112    * @throws NullPointerException if the given <code>MouseClickInfo</code> is <code>null</code>.
113    * @throws IllegalStateException if this fixture's <code>JTable</code> is disabled.
114    * @throws IllegalStateException if this fixture's <code>JTable</code> is not showing on the screen.
115    */
 
116  1 toggle public JTableCellFixture click(MouseClickInfo mouseClickInfo) {
117  1 table.click(cell, mouseClickInfo);
118  1 return this;
119    }
120   
121    /**
122    * Simulates a user double-clicking this fixture's table cell.
123    * @return this fixture.
124    * @throws IllegalStateException if this fixture's <code>JTable</code> is disabled.
125    * @throws IllegalStateException if this fixture's <code>JTable</code> is not showing on the screen.
126    */
 
127  1 toggle public JTableCellFixture doubleClick() {
128  1 return click(LEFT_BUTTON, 2);
129    }
130   
131    /**
132    * Simulates a user right-clicking this fixture's table cell.
133    * @return this fixture.
134    * @throws IllegalStateException if this fixture's <code>JTable</code> is disabled.
135    * @throws IllegalStateException if this fixture's <code>JTable</code> is not showing on the screen.
136    */
 
137  1 toggle public JTableCellFixture rightClick() {
138  1 return click(RIGHT_BUTTON);
139    }
140   
141    /**
142    * Simulates a user clicking a cell in this fixture's table cell once, using the specified mouse button.
143    * @param button the mouse button to use.
144    * @return this fixture.
145    * @throws NullPointerException if the given <code>MouseButton</code> is <code>null</code>.
146    * @throws IllegalStateException if this fixture's <code>JTable</code> is disabled.
147    * @throws IllegalStateException if this fixture's <code>JTable</code> is not showing on the screen.
148    */
 
149  2 toggle public JTableCellFixture click(MouseButton button) {
150  2 table.click(cell, button);
151  2 return this;
152    }
153   
 
154  1 toggle private JTableCellFixture click(MouseButton button, int times) {
155  1 table.click(cell, button, times);
156  1 return this;
157    }
158   
159    /**
160    * Starts editing this fixture's table cell. This method should be called <strong>before</strong> manipulating the
161    * <code>{@link Component}</code> returned by <code>{@link #editor()}</code>.
162    * <p>
163    * This method uses the <code>{@link JTableCellWriter}</code> from the <code>{@link JTableFixture}</code> that
164    * created this fixture.
165    * </p>
166    * @return this fixture.
167    * @throws IllegalStateException if this fixture's <code>JTable</code> is disabled.
168    * @throws IllegalStateException if this fixture's <code>JTable</code> is not showing on the screen.
169    * @throws IllegalStateException if this cell is not editable.
170    * @throws IndexOutOfBoundsException if any of the indices (row and column) is out of bounds.
171    * @throws ActionFailedException if this writer is unable to handle the underlying cell editor.
172    * @see JTableFixture#cellWriter(JTableCellWriter)
173    * @see JTableCellWriter
174    * @see #editor()
175    */
 
176  1 toggle public JTableCellFixture startEditing() {
177  1 driver().startCellEditing(target(), cell);
178  1 return this;
179    }
180   
181    /**
182    * Stops editing this fixture's table cell. This method should be called <strong>after</strong> manipulating the
183    * <code>{@link Component}</code> returned by <code>{@link #editor()}</code>.
184    * <p>
185    * This method uses the <code>{@link JTableCellWriter}</code> from the <code>{@link JTableFixture}</code> that
186    * created this fixture.
187    * </p>
188    * @return this fixture.
189    * @throws IllegalStateException if this fixture's <code>JTable</code> is disabled.
190    * @throws IllegalStateException if this fixture's <code>JTable</code> is not showing on the screen.
191    * @throws IllegalStateException if this cell is not editable.
192    * @throws IndexOutOfBoundsException if any of the indices (row and column) is out of bounds.
193    * @throws ActionFailedException if this writer is unable to handle the underlying cell editor.
194    * @see JTableFixture#cellWriter(JTableCellWriter)
195    * @see JTableCellWriter
196    * @see #editor()
197    */
 
198  1 toggle public JTableCellFixture stopEditing() {
199  1 driver().stopCellEditing(target(), cell);
200  1 return this;
201    }
202   
203    /**
204    * Cancels editing this fixture's table cell. This method should be called <strong>after</strong> manipulating the
205    * <code>{@link Component}</code> returned by <code>{@link #editor()}</code>.
206    * <p>
207    *
208    * <pre>
209    * TableCellFixture cell = table.cell(row(6).column(8));
210    * Component editor = cell.editor();
211    * // assume editor is a JTextField
212    * JTextComponentFixture editorFixture = new JTextComponentFixture(robot, (JTextField) editor);
213    * cell.{@link #startEditing()};
214    * editorFixture.enterText(&quot;Hello&quot;);
215    * // discard any entered value
216    * cell.cancelEditing();
217    * </pre>
218    *
219    * </p>
220    * <p>
221    * This method uses the <code>{@link JTableCellWriter}</code> from the <code>{@link JTableFixture}</code> that
222    * created this fixture.
223    * </p>
224    * @return this fixture.
225    * @throws IllegalStateException if this fixture's <code>JTable</code> is disabled.
226    * @throws IllegalStateException if this fixture's <code>JTable</code> is not showing on the screen.
227    * @throws IllegalStateException if this cell is not editable.
228    * @throws IndexOutOfBoundsException if any of the indices (row and column) is out of bounds.
229    * @throws ActionFailedException if this writer is unable to handle the underlying cell editor.
230    * @see JTableFixture#cellWriter(JTableCellWriter)
231    * @see JTableCellWriter
232    * @see #editor()
233    */
 
234  1 toggle public JTableCellFixture cancelEditing() {
235  1 driver().cancelCellEditing(target(), cell);
236  1 return this;
237    }
238   
239    /**
240    * Returns the editor of this fixture's table cell. To manipulate the editor (e.g. wrapping it with a
241    * <code>ComponentFixture</code>,) the method <code>{@link #startEditing()}</code> should be called first. To
242    * apply any changes back to the table cell, the method <code>{@link #stopEditing()}</code> should be called. This
243    * method uses the <code>{@link JTableCellWriter}</code> from the <code>{@link JTableFixture}</code> that created
244    * this fixture.
245    * <p>
246    * Example:
247    *
248    * <pre>
249    * TableCellFixture cell = table.cell(row(6).column(8));
250    * Component editor = cell.editor();
251    * // assume editor is a JTextField
252    * JTextComponentFixture editorFixture = new JTextComponentFixture(robot, (JTextField) editor);
253    * cell.{@link #startEditing()};
254    * editorFixture.enterText(&quot;Hello&quot;);
255    * cell.{@link #stopEditing()};
256    * </pre>
257    *
258    * </p>
259    * @return the editor of this fixture's table cell.
260    * @see JTableFixture#cellWriter(JTableCellWriter)
261    * @see JTableCellWriter
262    */
 
263  1 toggle public Component editor() {
264  1 return driver().cellEditor(target(), cell);
265    }
266   
267    /**
268    * Enters the given value to this fixture's table cell. This method starts cell edition, enters the given value and
269    * stops cell edition. To change the value of a cell, only a call to this method is necessary. If you need more
270    * flexibility, you can retrieve the cell editor with <code>{@link #editor()}</code>.
271    * <p>
272    * This method uses the <code>{@link JTableCellWriter}</code> from the <code>{@link JTableFixture}</code> that
273    * created this fixture.
274    * </p>
275    * @param value the value to enter in the cell.
276    * @return this fixture.
277    * @throws IllegalStateException if this fixture's <code>JTable</code> is disabled.
278    * @throws IllegalStateException if this fixture's <code>JTable</code> is not showing on the screen.
279    * @throws IllegalStateException if this cell is not editable.
280    * @throws IndexOutOfBoundsException if any of the indices (row and column) is out of bounds.
281    * @throws ActionFailedException if this driver's <code>JTableCellValueReader</code> is unable to enter the given
282    * value.
283    * @see JTableFixture#cellWriter(JTableCellWriter)
284    * @see JTableCellWriter
285    */
 
286  5 toggle public JTableCellFixture enterValue(String value) {
287  5 driver().enterValueInCell(target(), cell, value);
288  5 return this;
289    }
290   
 
291  9 toggle private JTableDriver driver() { return table.driver(); }
 
292  9 toggle private JTable target() { return table.target; }
293   
294    /**
295    * Asserts that the value of this fixture's table cell matches the given value.
296    * @param value the expected value of this fixture's table cell. It can be a regular expression.
297    * @return this fixture.
298    * @throws AssertionError if the value of this fixture's table cell does not match the expected one.
299    */
 
300  3 toggle public JTableCellFixture requireValue(String value) {
301  3 table.requireCellValue(cell, value);
302  3 return this;
303    }
304   
305    /**
306    * Asserts that the value of this fixture's table cell matches the given regular expression pattern.
307    * @param pattern the regular expression pattern to match.
308    * @return this fixture.
309    * @throws NullPointerException if the given regular expression pattern is <code>null</code>.
310    * @throws AssertionError if the value of this fixture's table cell does not match the expected the given regular
311    * expression pattern.
312    * @since 1.2
313    */
 
314  1 toggle public JTableCellFixture requireValue(Pattern pattern) {
315  1 table.requireCellValue(cell, pattern);
316  1 return this;
317    }
318   
319    /**
320    * Returns a fixture that verifies the font of this fixture's table cell. This method uses the
321    * <code>{@link JTableCellReader}</code> from the <code>{@link JTableFixture}</code> that created this fixture.
322    * @return a fixture that verifies the font of this fixture's table cell.
323    * @see JTableFixture#cellReader(JTableCellReader)
324    * @see JTableCellReader
325    */
 
326  1 toggle public FontFixture font() {
327  1 return table.fontAt(cell);
328    }
329   
330    /**
331    * Returns a fixture that verifies the background color of this fixture's table cell. This method uses the
332    * <code>{@link JTableCellReader}</code> from the <code>{@link JTableFixture}</code> that created this fixture.
333    * @return a fixture that verifies the background color of this fixture's table cell.
334    * @see JTableFixture#cellReader(JTableCellReader)
335    * @see JTableCellReader
336    */
 
337  1 toggle public ColorFixture background() {
338  1 return table.backgroundAt(cell);
339    }
340   
341    /**
342    * Returns a fixture that verifies the foreground color of this fixture's table cell. This method uses the
343    * <code>{@link JTableCellReader}</code> from the <code>{@link JTableFixture}</code> that created this fixture.
344    * @return a fixture that verifies the foreground color of this fixture's table cell.
345    * @see JTableFixture#cellReader(JTableCellReader)
346    * @see JTableCellReader
347    */
 
348  1 toggle public ColorFixture foreground() {
349  1 return table.foregroundAt(cell);
350    }
351   
352    /**
353    * Returns the <code>String</code> representation of the value of this fixture's table cell. This method uses the
354    * <code>{@link JTableCellReader}</code> from the <code>{@link JTableFixture}</code> that created this fixture.
355    * @return the <code>String</code> representation of the value of this fixture's table cell.
356    * @see JTableFixture#cellReader(JTableCellReader)
357    * @see JTableCellReader
358    */
 
359  1 toggle public String value() {
360  1 return table.valueAt(cell);
361    }
362   
363    /**
364    * Simulates a user dragging this fixture's table cell.
365    * @return this fixture.
366    */
 
367  1 toggle public JTableCellFixture drag() {
368  1 table.drag(cell);
369  1 return this;
370    }
371   
372    /**
373    * Simulates a user dropping into this fixture's table cell.
374    * @return this fixture.
375    */
 
376  1 toggle public JTableCellFixture drop() {
377  1 table.drop(cell);
378  1 return this;
379    }
380   
381    /**
382    * Shows a pop-up menu using this fixture's table cell as the invoker of the pop-up menu.
383    * @return a fixture that manages the displayed pop-up menu.
384    * @throws ComponentLookupException if a pop-up menu cannot be found.
385    */
 
386  1 toggle public JPopupMenuFixture showPopupMenu() {
387  1 return table.showPopupMenuAt(cell);
388    }
389   
390    /**
391    * Asserts that this fixture's table cell is editable.
392    * @return this fixture.
393    * @throws AssertionError if this fixture's table cell is not editable.
394    */
 
395  2 toggle public JTableCellFixture requireEditable() {
396  2 table.requireEditable(cell);
397  2 return this;
398    }
399   
400   
401    /**
402    * Asserts that this fixture's table cell is not editable.
403    * @return this fixture.
404    * @throws AssertionError if this fixture's table cell is editable.
405    */
 
406  2 toggle public JTableCellFixture requireNotEditable() {
407  2 table.requireNotEditable(cell);
408  2 return this;
409    }
410   
411    /**
412    * Returns the row index of this fixture's table cell.
413    * @return the row index of this fixture's table cell.
414    */
 
415  4 toggle public int row() { return cell.row; }
416   
417    /**
418    * Returns the column index of this fixture's table cell.
419    * @return the column index of this fixture's table cell.
420    */
 
421  3 toggle public int column() { return cell.column; }
422   
423    }