001: package abbot.tester;
002:
003: import java.awt.Component;
004: import java.awt.Dimension;
005: import java.util.Vector;
006:
007: import javax.swing.*;
008:
009: import javax.swing.table.TableCellRenderer;
010:
011: import junit.extensions.abbot.*;
012:
013: /** Unit test to verify the JTableTester class.<p> */
014:
015: public class JTableTesterTest extends ComponentTestFixture {
016:
017: private int ROWS = 4;
018: private int COLS = 4;
019: private JTableTester tester;
020: private JTable table;
021:
022: /** Create a new test case with the given name. */
023: public JTableTesterTest(String name) {
024: super (name);
025: }
026:
027: protected void setUp() {
028: String[][] data = new String[][] {
029: { "0 one", "0 two", "0 three", "0 four" },
030: { "1 one", "1 two", "1 three", "1 four" },
031: { "2 one", "2 two", "2 three", "2 four" },
032: { "3 one", "3 two", "3 three", "3 four" }, };
033: String[] names = { "one", "two", "three", "four" };
034: table = new JTable(data, names);
035: tester = new JTableTester();
036: }
037:
038: public void testSelectCells() {
039: showFrame(table);
040: for (int row = 0; row < ROWS; row++) {
041: for (int col = 0; col < COLS; col++) {
042: tester.actionClick(table, new JTableLocation(row, col));
043: assertEquals("Wrong row selected", row, table
044: .getSelectedRow());
045: assertEquals("Wrong column selected", col, table
046: .getSelectedColumn());
047: }
048: }
049: }
050:
051: /** Ensure scrolling works. */
052: public void testScrollToVisible() {
053: Vector data = new Vector();
054: Vector columnNames = new Vector();
055:
056: int ROWS = 100;
057: int COLS = 4;
058: for (int row = 0; row < ROWS; row++) {
059: Vector rowv = new Vector();
060: for (int col = 0; col < COLS; col++) {
061: rowv.add(String.valueOf(row + 1) + ","
062: + String.valueOf(col + 1));
063: }
064: data.add(rowv);
065: }
066: for (int col = 0; col < COLS; col++) {
067: columnNames.add(String.valueOf(col));
068: }
069: table = new JTable(data, columnNames);
070: Dimension size = new Dimension(150, 50);
071: table.setPreferredScrollableViewportSize(size);
072: JScrollPane scrollPane = new JScrollPane(table);
073: showFrame(scrollPane);
074: tester.actionClick(table, new JTableLocation(0, 0));
075: assertEquals("Wrong row selected", 0, table.getSelectedRow());
076: assertEquals("Wrong column selected", 0, table
077: .getSelectedColumn());
078: tester.actionClick(table,
079: new JTableLocation(ROWS - 1, COLS - 1));
080: assertEquals("Wrong row selected", ROWS - 1, table
081: .getSelectedRow());
082: assertEquals("Wrong column selected", COLS - 1, table
083: .getSelectedColumn());
084: tester.actionClick(table, new JTableLocation(ROWS - 1, 0));
085: assertEquals("Wrong row selected", ROWS - 1, table
086: .getSelectedRow());
087: assertEquals("Wrong column selected", 0, table
088: .getSelectedColumn());
089: tester.actionClick(table, new JTableLocation(0, COLS - 1));
090: assertEquals("Wrong row selected", 0, table.getSelectedRow());
091: assertEquals("Wrong column selected", COLS - 1, table
092: .getSelectedColumn());
093: tester.actionClick(table, new JTableLocation(0, 0));
094: assertEquals("Wrong row selected", 0, table.getSelectedRow());
095: assertEquals("Wrong column selected", 0, table
096: .getSelectedColumn());
097: }
098:
099: /** Ensure custom renderers work **/
100: public void testGetTextRenderer() {
101:
102: class OtherRenderer extends JTextField implements
103: TableCellRenderer {
104:
105: public Component getTableCellRendererComponent(
106: JTable table, Object value, boolean isSelected,
107: boolean hasFocus, int row, int column) {
108: setText(value.toString());
109: return this ;
110: }
111: }
112:
113: //
114:
115: table.setDefaultRenderer(String.class, new OtherRenderer());
116:
117: assertEquals(
118: "Must return the correct child text for non JLabel renderers",
119: JTableTester.valueToString(table, 0, 0), "0 one");
120: assertEquals(
121: "Must return the correct child text for non JLabel renderers",
122: JTableTester.valueToString(table, 3, 3), "3 four");
123:
124: }
125:
126: public static void main(String[] args) {
127: RepeatHelper.runTests(args, JTableTesterTest.class);
128: }
129: }
|