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.XmlAssert;
007: import org.uispec4j.xml.EventLogger;
008:
009: import javax.swing.DefaultListCellRenderer;
010: import javax.swing.JButton;
011: import javax.swing.JList;
012: import javax.swing.ListCellRenderer;
013: import javax.swing.event.ListSelectionListener;
014: import javax.swing.event.ListSelectionEvent;
015: import java.awt.Component;
016: import java.awt.event.KeyEvent;
017: import java.awt.event.KeyListener;
018:
019: public class ListBoxTest extends UIComponentTestCase {
020: private static final String[] ALL_ITEMS = new String[] {
021: "First Item", "Second Item", "Third Item" };
022:
023: private JList jList;
024: private ListBox listBox;
025:
026: protected void setUp() throws Exception {
027: super .setUp();
028: init(new JList(new Object[] { "First Item", "Second Item",
029: "Third Item" }));
030: }
031:
032: private void init(JList list) {
033: jList = list;
034: jList.setName("myList");
035: listBox = new ListBox(jList);
036: }
037:
038: public void testGetComponentTypeName() throws Exception {
039: assertEquals("listBox", listBox.getDescriptionTypeName());
040: }
041:
042: public void testGetDescription() throws Exception {
043: XmlAssert.assertEquivalent("<listBox name='myList'/>", listBox
044: .getDescription());
045: }
046:
047: public void testFactory() throws Exception {
048: checkFactory(new JList(), ListBox.class);
049: }
050:
051: protected UIComponent createComponent() {
052: return listBox;
053: }
054:
055: public void testEmptyList() throws Exception {
056: init(new JList());
057: assertTrue(listBox.isEmpty());
058: }
059:
060: public void testAssertEmptyFailure() throws Exception {
061: try {
062: assertTrue(listBox.isEmpty());
063: throw new AssertionFailureNotDetectedError();
064: } catch (AssertionFailedError e) {
065: assertEquals(
066: "List should be empty but contains: [First Item,Second Item,Third Item]",
067: e.getMessage());
068: }
069: }
070:
071: public void testContentEquals() throws Exception {
072: assertTrue(listBox.contentEquals(ALL_ITEMS));
073: }
074:
075: public void testContentEqualsWithNullItem() throws Exception {
076: init(new JList(new Object[] { "First Item", null }));
077: listBox.contentEquals(new String[] { "First Item", "" });
078: }
079:
080: public void testContentEqualsErrors() throws Exception {
081: try {
082: assertTrue(listBox.contentEquals(new String[] { "another",
083: "list", "here" }));
084: throw new AssertionFailureNotDetectedError();
085: } catch (AssertionFailedError e) {
086: }
087: try {
088: assertTrue(listBox.contentEquals(new String[] { "another",
089: "list", "here", "with", "more", "elements" }));
090: throw new AssertionFailureNotDetectedError();
091: } catch (AssertionFailedError e) {
092: }
093: try {
094: assertTrue(listBox
095: .contentEquals(new String[] { "one element only" }));
096: throw new AssertionFailureNotDetectedError();
097: } catch (AssertionFailedError e) {
098: }
099: }
100:
101: public void testContains() throws Exception {
102: init(new JList(new Object[] { "one", "two", "three" }));
103: assertTrue(listBox.contains("two"));
104: assertTrue(listBox.contains(new String[] { "three", "one" }));
105: }
106:
107: public void testContainsErrors() throws Exception {
108: init(new JList(new Object[] { "one", "two", "three" }));
109: try {
110: assertTrue(listBox.contains("unknown"));
111: throw new AssertionFailureNotDetectedError();
112: } catch (AssertionFailedError e) {
113: assertEquals(
114: "Item 'unknown' not found - actual content:[one, two, three]",
115: e.getMessage());
116: }
117:
118: try {
119: assertTrue(listBox.contains(new String[] { "three",
120: "unknown" }));
121: throw new AssertionFailureNotDetectedError();
122: } catch (AssertionFailedError e) {
123: assertEquals(
124: "Item 'unknown' not found - actual content:[one, two, three]",
125: e.getMessage());
126: }
127: }
128:
129: public void testSelectByIndex() throws Exception {
130: assertEquals(-1, jList.getSelectedIndex());
131: listBox.selectIndices(new int[] { 2 });
132: assertEquals(2, jList.getSelectedIndex());
133: }
134:
135: public void testSelectByName() throws Exception {
136: checkSelectionByName(new String[] { "First Item",
137: "Second Item", "Third Item" });
138: }
139:
140: public void testSelectionIsNotCaseSensitive() throws Exception {
141: checkSelectionByName(new String[] { "first iteM",
142: "second Item", "THIRD iTem" });
143: }
144:
145: public void testSelectionWithSubstring() throws Exception {
146: checkSelectionByName(new String[] { "first", "second", "ird it" });
147: }
148:
149: public void testAmbiguityInSelection() throws Exception {
150: try {
151: listBox.select("item");
152: throw new AssertionFailureNotDetectedError();
153: } catch (ItemAmbiguityException e) {
154: assertEquals(
155: "3 items are matching the same pattern 'item': [First Item,Second Item,Third Item]",
156: e.getMessage());
157: }
158: }
159:
160: public void testSelectingAnUnknownValueThrowsAnException()
161: throws Exception {
162: try {
163: listBox.select("unknown");
164: throw new AssertionFailureNotDetectedError();
165: } catch (AssertionFailedError e) {
166: }
167: }
168:
169: public void testClearSelection() throws Exception {
170: jList.setSelectedIndex(1);
171: listBox.clearSelection();
172: assertTrue(listBox.selectionIsEmpty());
173: }
174:
175: public void testSelectByNameThrowsAnExceptionIfTheItemIsNotFound()
176: throws Exception {
177: try {
178: listBox.select("unknown");
179: throw new AssertionFailureNotDetectedError();
180: } catch (AssertionFailedError e) {
181: assertEquals(
182: "Item 'unknown' not found in [First Item,Second Item,Third Item]",
183: e.getMessage());
184: }
185: }
186:
187: public void testMultiSelectionWithIndices() throws Exception {
188: listBox.selectIndices(new int[] { 0, 1 });
189:
190: int[] selectedIndices = jList.getSelectedIndices();
191: assertEquals(2, selectedIndices.length);
192: assertEquals(0, selectedIndices[0]);
193: assertEquals(1, selectedIndices[1]);
194: }
195:
196: public void testMultiSelectionWithNames() throws Exception {
197: listBox.select(new String[] { "first", "second" });
198:
199: int[] selectedIndices = jList.getSelectedIndices();
200: assertEquals(2, selectedIndices.length);
201: assertEquals(0, selectedIndices[0]);
202: assertEquals(1, selectedIndices[1]);
203: }
204:
205: public void testMultiSelectionSetsTheAdjustingMode()
206: throws Exception {
207: final EventLogger logger = new EventLogger();
208: jList.getSelectionModel().addListSelectionListener(
209: new ListSelectionListener() {
210: public void valueChanged(ListSelectionEvent event) {
211: if (!event.getValueIsAdjusting()) {
212: logger.log("valueChanged");
213: }
214: }
215: });
216: listBox.select(new String[] { "first", "second" });
217: logger.assertEquals("<log><valueChanged/></log>");
218: listBox.selectIndices(new int[] { 0, 1 });
219: logger.assertEquals("<log><valueChanged/></log>");
220: }
221:
222: public void testMultiSelectionWithNamesWhenSomeNamesDoNotMatch()
223: throws Exception {
224: listBox.select(new String[] { "first", "third", "fourth" });
225:
226: int[] selectedIndices = jList.getSelectedIndices();
227: assertEquals(2, selectedIndices.length);
228: assertEquals(0, selectedIndices[0]);
229: assertEquals(2, selectedIndices[1]);
230: }
231:
232: public void testGetSize() throws Exception {
233: assertEquals(3, listBox.getSize());
234: }
235:
236: public void testAssertSelectionEmpty() throws Exception {
237: jList.setSelectedIndex(-1);
238: assertTrue(listBox.selectionIsEmpty());
239: jList.setSelectedIndex(1);
240: try {
241: assertTrue(listBox.selectionIsEmpty());
242: throw new AssertionFailureNotDetectedError();
243: } catch (AssertionFailedError e) {
244: assertEquals(
245: "Selection should be empty but is: [Second Item]",
246: e.getMessage());
247: }
248: }
249:
250: public void testAssertSelection() throws Exception {
251: jList.setSelectedIndex(0);
252: assertTrue(listBox.selectionEquals("First Item"));
253: jList.setSelectedIndex(1);
254: assertTrue(listBox.selectionEquals("Second Item"));
255: jList.setSelectedIndices(new int[] { 0, 2 });
256: assertTrue(listBox.selectionEquals(new String[] { "First Item",
257: "Third Item" }));
258: }
259:
260: public void testAssertSelectionErrors() throws Exception {
261: jList.setSelectedIndex(0);
262: try {
263: assertTrue(listBox.selectionEquals("Second Item"));
264: throw new AssertionFailureNotDetectedError();
265: } catch (AssertionFailedError e) {
266: assertEquals("Expected: [Second Item]\n"
267: + "Actual: [First Item]", e.getMessage());
268: }
269:
270: try {
271: assertTrue(listBox.selectionEquals(new String[] {
272: "First Item", "Third Item" }));
273: throw new AssertionFailureNotDetectedError();
274: } catch (AssertionFailedError e) {
275: assertEquals("Expected: [First Item,Third Item]\n"
276: + "Actual: [First Item]", e.getMessage());
277: }
278: }
279:
280: public void testPressingKeyForNavigatingInTheList()
281: throws Exception {
282: jList.setSelectedIndex(0);
283: assertTrue(listBox.selectionEquals("First Item"));
284: listBox.pressKey(Key.DOWN);
285: assertTrue(listBox.selectionEquals("Second Item"));
286: listBox.pressKey(Key.DOWN);
287: assertTrue(listBox.selectionEquals("Third Item"));
288: listBox.pressKey(Key.UP);
289: assertTrue(listBox.selectionEquals("Second Item"));
290: }
291:
292: public void testUsingShiftToSelectMultipleElements()
293: throws Exception {
294: jList.setSelectedIndex(0);
295: listBox.pressKey(Key.shift(Key.PAGE_DOWN));
296: assertTrue(listBox.selectionEquals(ALL_ITEMS));
297: }
298:
299: public void testPressingKeyNotifiesCustomKeyListeners()
300: throws Exception {
301: final Counter counter = new Counter();
302: jList.addKeyListener(new KeyListener() {
303: public void keyPressed(KeyEvent event) {
304: counter.increment();
305: }
306:
307: public void keyReleased(KeyEvent event) {
308: }
309:
310: public void keyTyped(KeyEvent event) {
311: }
312: });
313: jList.setSelectedIndex(0);
314: assertTrue(listBox.selectionEquals("First Item"));
315: listBox.pressKey(Key.DOWN);
316: assertEquals(1, counter.getCount());
317: assertTrue(listBox.selectionEquals("Second Item"));
318: }
319:
320: public void testUsingARenderer() throws Exception {
321: init(new JList(new Object[] { new Integer(3), new Integer(7),
322: new Integer(11) }));
323: jList.setCellRenderer(new DefaultListCellRenderer() {
324: public Component getListCellRendererComponent(JList list,
325: Object value, int index, boolean isSelected,
326: boolean cellHasFocus) {
327: return super .getListCellRendererComponent(list, "-"
328: + value, index, isSelected, cellHasFocus);
329: }
330: });
331: assertTrue(listBox.contentEquals(new String[] { "-3", "-7",
332: "-11" }));
333: listBox.selectIndex(0);
334: assertTrue(listBox.selectionEquals("-3"));
335: listBox.select("-7");
336: assertTrue(listBox.selectionEquals("-7"));
337: }
338:
339: public void testUsingACustomCellRenderer() throws Exception {
340: init(new JList(new Object[] { new Integer(3), new Integer(7) }));
341: jList.setCellRenderer(new ListCellRenderer() {
342: public Component getListCellRendererComponent(JList list,
343: Object value, int index, boolean isSelected,
344: boolean cellHasFocus) {
345: return new JButton("-" + value);
346: }
347: });
348: listBox.setCellValueConverter(new ListBoxCellValueConverter() {
349: public String getValue(int index,
350: Component renderedComponent, Object modelObject) {
351: return ((JButton) renderedComponent).getText();
352: }
353: });
354: assertTrue(listBox.contentEquals(new String[] { "-3", "-7" }));
355: listBox.selectIndex(0);
356: assertTrue(listBox.selectionEquals("-3"));
357: listBox.select("-7");
358: assertTrue(listBox.selectionEquals("-7"));
359: }
360:
361: public void testAssertContentEqualsAfterSettingACustomCellValueConverter()
362: throws Exception {
363: assertTrue(listBox.contentEquals(ALL_ITEMS));
364:
365: listBox.setCellValueConverter(new ListBoxCellValueConverter() {
366: public String getValue(int index,
367: Component renderedComponent, Object modelObject) {
368: if (index == 0) {
369: return "Item converted";
370: }
371: return modelObject.toString();
372: }
373: });
374: assertTrue(listBox.contentEquals(new String[] {
375: "Item converted", "Second Item", "Third Item" }));
376:
377: try {
378: assertTrue(listBox.contentEquals(ALL_ITEMS));
379: throw new AssertionFailureNotDetectedError();
380: } catch (AssertionFailedError e) {
381: // Expected
382: }
383:
384: try {
385: assertTrue(listBox.isEmpty());
386: throw new AssertionFailureNotDetectedError();
387: } catch (AssertionFailedError e) {
388: assertEquals(
389: "List should be empty but contains: [Item converted,Second Item,Third Item]",
390: e.getMessage());
391: }
392: }
393:
394: public void testSelectionAfterSettingACustomCellValueConverter()
395: throws Exception {
396: assertTrue(listBox.contentEquals(ALL_ITEMS));
397:
398: listBox.setCellValueConverter(new ListBoxCellValueConverter() {
399: public String getValue(int index,
400: Component renderedComponent, Object modelObject) {
401: if (index == 0) {
402: return "Item converted";
403: }
404: return modelObject.toString();
405: }
406: });
407:
408: listBox.select("Item converted");
409:
410: assertTrue(listBox.selectionEquals("Item converted"));
411: assertTrue(listBox
412: .selectionEquals(new String[] { "Item converted" }));
413:
414: try {
415: listBox.select("First Item");
416: throw new AssertionFailureNotDetectedError();
417: } catch (AssertionFailedError e) {
418: assertEquals(
419: "Item 'First Item' not found in [Item converted,Second Item,Third Item]",
420: e.getMessage());
421: }
422: }
423:
424: private void checkSelectionByName(String[] names) {
425: listBox.select(names[0]);
426: assertTrue(listBox.selectionEquals("First Item"));
427: assertEquals(0, jList.getSelectedIndex());
428: listBox.select(names[1]);
429: assertTrue(listBox.selectionEquals("Second Item"));
430: assertEquals(1, jList.getSelectedIndex());
431: listBox.select(names[2]);
432: assertTrue(listBox.selectionEquals("Third Item"));
433: assertEquals(2, jList.getSelectedIndex());
434: }
435: }
|