001: package org.uispec4j;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.utils.AssertionFailureNotDetectedError;
005: import org.uispec4j.utils.Counter;
006: import org.uispec4j.xml.EventLogger;
007:
008: import javax.swing.event.ListSelectionListener;
009: import javax.swing.event.ListSelectionEvent;
010: import java.awt.event.MouseAdapter;
011: import java.awt.event.MouseEvent;
012:
013: public class TableSelectionTest extends TableTestCase {
014:
015: public void testClickCallsMouseListeners() throws Exception {
016: MouseLogger mouseLogger = new MouseLogger(jTable);
017: table.click(0, 1);
018: mouseLogger.assertEquals("<log>"
019: + " <mousePressed button='1'/>"
020: + " <mouseReleased button='1'/>"
021: + " <mouseClicked button='1'/>" + "</log>");
022: }
023:
024: public void testAssertSelectionEquals() throws Exception {
025: jTable.setCellSelectionEnabled(true);
026: table.click(0, 1);
027: assertTrue(table.selectionEquals(new boolean[][] {
028: { false, true, false }, { false, false, false } }));
029:
030: table.click(1, 1, Key.Modifier.CONTROL);
031: assertTrue(table.selectionEquals(new boolean[][] {
032: { false, true, false }, { false, true, false } }));
033:
034: try {
035: assertTrue(table.selectionEquals(new boolean[][] {
036: { false, false, false }, { false, false, false } }));
037: throw new AssertionFailureNotDetectedError();
038: } catch (AssertionFailedError e) {
039: assertEquals(
040: "expected:<[[false,false,false], [false,false,false]]> "
041: + "but was:<[[false,true,false], [false,true,false]]>",
042: e.getMessage());
043: }
044: }
045:
046: public void testAssertRowSelected() throws Exception {
047: jTable.setCellSelectionEnabled(false);
048: table.click(0, 1);
049:
050: assertTrue(table.rowIsSelected(0));
051: assertFalse(table.rowIsSelected(1));
052:
053: try {
054: assertTrue(table.rowIsSelected(1));
055: throw new AssertionFailureNotDetectedError();
056: } catch (AssertionFailedError e) {
057: }
058:
059: try {
060: assertFalse(table.rowIsSelected(0));
061: throw new AssertionFailureNotDetectedError();
062: } catch (AssertionFailedError e) {
063: }
064: }
065:
066: public void testAssertRowsSelected() throws Exception {
067: checkAssertRowsSelected(false);
068: checkAssertRowsSelected(true);
069: }
070:
071: private void checkAssertRowsSelected(boolean cellSelectionEnabled) {
072: jTable.setCellSelectionEnabled(cellSelectionEnabled);
073: jTable.clearSelection();
074: assertTrue(table.rowsAreSelected(new int[] {}));
075:
076: table.click(0, 1);
077: assertTrue(table.rowsAreSelected(new int[] { 0 }));
078:
079: table.click(1, 1);
080: assertTrue(table.rowsAreSelected(new int[] { 1 }));
081:
082: table.click(0, 1, Key.Modifier.CONTROL);
083: assertTrue(table.rowsAreSelected(new int[] { 0, 1 }));
084: assertTrue(table.rowsAreSelected(new int[] { 1, 0 }));
085:
086: try {
087: assertTrue(table.rowsAreSelected(new int[] { 0 }));
088: throw new AssertionFailureNotDetectedError();
089: } catch (AssertionFailedError e) {
090: assertEquals("Expected: [0]\nActual: [0,1]", e
091: .getMessage());
092: }
093: }
094:
095: public void testDoubleClickChangesSelection() throws Exception {
096: final Counter doubleClickCounter = new Counter();
097: jTable.addMouseListener(new MouseAdapter() {
098: public void mouseClicked(MouseEvent e) {
099: if (e.getClickCount() == 2) {
100: doubleClickCounter.increment();
101: }
102: }
103: });
104: jTable.setCellSelectionEnabled(true);
105: table.selectCell(1, 0);
106:
107: assertTrue(table.selectionEquals(new boolean[][] {
108: { false, false, false }, { true, false, false } }));
109:
110: assertEquals(0, doubleClickCounter.getCount());
111: table.doubleClick(0, 1);
112: assertEquals(1, doubleClickCounter.getCount());
113:
114: assertTrue(table.selectionEquals(new boolean[][] {
115: { false, true, false }, { false, false, false } }));
116: }
117:
118: public void testAssertCellSelected() throws Exception {
119: jTable.setCellSelectionEnabled(true);
120: table.click(0, 1);
121:
122: assertTrue(table.cellIsSelected(0, 1));
123: assertFalse(table.cellIsSelected(1, 1));
124:
125: try {
126: assertTrue(table.cellIsSelected(1, 1));
127: throw new AssertionFailureNotDetectedError();
128: } catch (AssertionFailedError e) {
129: }
130:
131: try {
132: assertFalse(table.cellIsSelected(0, 1));
133: throw new AssertionFailureNotDetectedError();
134: } catch (AssertionFailedError e) {
135: }
136: }
137:
138: public void testAssertCellSelectedWithRowLevelSelection()
139: throws Exception {
140: jTable.setCellSelectionEnabled(false);
141: table.click(0, 1);
142:
143: try {
144: assertTrue(table.cellIsSelected(0, 1));
145: throw new AssertionFailureNotDetectedError();
146: } catch (AssertionFailedError e) {
147: assertEquals(
148: "Cell-level selection is not supported on this table",
149: e.getMessage());
150: }
151: }
152:
153: public void testAssertSelectionIsEmpty() throws Exception {
154: assertTrue(table.selectionIsEmpty());
155: table.click(1, 0);
156: try {
157: assertTrue(table.selectionIsEmpty());
158: throw new AssertionFailureNotDetectedError();
159: } catch (AssertionFailedError e) {
160: assertEquals("Selection is not empty", e.getMessage());
161: }
162: table.clearSelection();
163: table.selectionIsEmpty();
164: }
165:
166: public void testClearSelection() throws Exception {
167: jTable.addRowSelectionInterval(1, 1);
168: table.clearSelection();
169: assertTrue(table.selectionIsEmpty());
170: }
171:
172: public void testSelectCell() throws Exception {
173: jTable.setCellSelectionEnabled(true);
174: table.selectCell(0, 0);
175: assertTrue(table.selectionEquals(new boolean[][] {
176: { true, false, false }, { false, false, false } }));
177: table.selectCell(1, 2);
178: assertTrue(table.selectionEquals(new boolean[][] {
179: { false, false, false }, { false, false, true } }));
180: }
181:
182: public void testSelectCellWithCellSelectionDisabled()
183: throws Exception {
184: jTable.setCellSelectionEnabled(false);
185: try {
186: table.selectCell(0, 0);
187: throw new AssertionFailureNotDetectedError();
188: } catch (AssertionFailedError e) {
189: assertEquals(
190: "Individual cell selection is not allowed on this table",
191: e.getMessage());
192: }
193: }
194:
195: public void testSelectRow() throws Exception {
196: checkSelectRow(false);
197: checkSelectRow(true);
198: }
199:
200: private void checkSelectRow(boolean cellSelectionEnabled) {
201: jTable.setCellSelectionEnabled(cellSelectionEnabled);
202: table.selectRow(0);
203: checkRowSelection(true, false);
204:
205: table.selectRow(1);
206: checkRowSelection(false, true);
207: }
208:
209: public void testSelectRows() throws Exception {
210: checkSelectRows(false);
211: checkSelectRows(true);
212: }
213:
214: private void checkSelectRows(boolean cellSelectionEnabled) {
215: jTable.setCellSelectionEnabled(cellSelectionEnabled);
216: table.selectRows(new int[] { 0 });
217: checkRowSelection(true, false);
218:
219: table.selectRows(new int[] { 1 });
220: checkRowSelection(false, true);
221:
222: table.selectRows(new int[] { 0, 1 });
223: checkRowSelection(true, true);
224:
225: table.selectRows(0, 0);
226: checkRowSelection(true, false);
227:
228: table.selectRows(1, 1);
229: checkRowSelection(false, true);
230:
231: table.selectRows(0, 1);
232: checkRowSelection(true, true);
233: }
234:
235: public void testSelectRowsRequiresThatTheStartIndexBeLessThanTheEndIndex()
236: throws Exception {
237: try {
238: table.selectRows(1, 0);
239: throw new AssertionFailureNotDetectedError();
240: } catch (IllegalArgumentException e) {
241: assertEquals("Invalid indexes: 1 > 0", e.getMessage());
242: }
243: }
244:
245: public void testSelectBlock() throws Exception {
246: jTable.setCellSelectionEnabled(true);
247: table.selectBlock(0, 0, 1, 1);
248: table.selectionEquals(new boolean[][] { { true, true, false },
249: { true, true, false } });
250: table.selectBlock(0, 1, 0, 2);
251: table.selectionEquals(new boolean[][] { { false, true, true },
252: { false, false, false } });
253: table.selectBlock(1, 1, 1, 1);
254: table.selectionEquals(new boolean[][] {
255: { false, false, false }, { false, true, false } });
256: }
257:
258: public void testSelectBlockWithInvalidRectangle() throws Exception {
259: jTable.setCellSelectionEnabled(true);
260: try {
261: table.selectBlock(1, 1, 0, 0);
262: throw new AssertionFailureNotDetectedError();
263: } catch (IllegalArgumentException e) {
264: assertEquals(
265: "Invalid block definition - expected top <= bottom and left <= right",
266: e.getMessage());
267: }
268: }
269:
270: public void testSelectBlockWithJTableCellSelectionDisabled()
271: throws Exception {
272: jTable.setCellSelectionEnabled(false);
273: try {
274: table.selectBlock(0, 0, 1, 1);
275: throw new AssertionFailureNotDetectedError();
276: } catch (AssertionFailedError e) {
277: assertEquals(
278: "Only row-level selection is allowed on this table",
279: e.getMessage());
280: }
281: }
282:
283: public void testAddRemoveSelectedRow() throws Exception {
284: checkAddRemoveSelectedRows(false);
285: checkAddRemoveSelectedRows(true);
286: }
287:
288: public void testRemoveRowFromSelectionChecksThatTheRowWasSelected()
289: throws Exception {
290: try {
291: table.removeRowFromSelection(1);
292: throw new AssertionFailureNotDetectedError();
293: } catch (AssertionFailedError e) {
294: assertEquals("Row 1 is not selected", e.getMessage());
295: }
296: }
297:
298: public void testMultiSelectionUpdatedTheAValueIsAdjustingMode()
299: throws Exception {
300: final EventLogger logger = new EventLogger();
301: jTable.getSelectionModel().addListSelectionListener(
302: new ListSelectionListener() {
303: public void valueChanged(ListSelectionEvent event) {
304: if (!event.getValueIsAdjusting()) {
305: logger.log("valueChanged");
306: }
307: }
308: });
309:
310: table.selectRows(new int[] { 0, 1 });
311: logger.assertEquals("<log><valueChanged/></log>");
312: table.selectRows(0, 1);
313: logger.assertEquals("<log><valueChanged/></log>");
314: }
315:
316: private void checkAddRemoveSelectedRows(boolean cellSelectionEnabled) {
317: jTable.setCellSelectionEnabled(cellSelectionEnabled);
318: table.clearSelection();
319: table.addRowToSelection(0);
320: checkRowSelection(true, false);
321:
322: table.addRowToSelection(1);
323: checkRowSelection(true, true);
324:
325: table.removeRowFromSelection(0);
326: checkRowSelection(false, true);
327:
328: table.removeRowFromSelection(1);
329: checkRowSelection(false, false);
330: }
331:
332: private void checkRowSelection(boolean row0Selected,
333: boolean row1Selected) {
334: boolean[] trues = { true, true, true };
335: boolean[] falses = { false, false, false };
336: boolean[][] expected = new boolean[2][3];
337: expected[0] = row0Selected ? trues : falses;
338: expected[1] = row1Selected ? trues : falses;
339: assertTrue(table.selectionEquals(expected));
340: }
341: }
|