001: package org.uispec4j;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.utils.AssertionFailureNotDetectedError;
005: import org.uispec4j.utils.Functor;
006:
007: import javax.swing.*;
008: import java.awt.*;
009:
010: public class ComboBoxTest extends UIComponentTestCase {
011: private ComboBox comboBox;
012: private JComboBox jComboBox;
013:
014: protected void setUp() throws Exception {
015: super .setUp();
016: init(new JComboBox(new String[] { "one", "two", "three" }));
017: }
018:
019: private void init(JComboBox box) {
020: jComboBox = box;
021: jComboBox.setName("marcel");
022: comboBox = new ComboBox(jComboBox);
023: }
024:
025: public void testGetComponentTypeName() throws Exception {
026: assertEquals("comboBox", comboBox.getDescriptionTypeName());
027: }
028:
029: public void testGetDescription() throws Exception {
030: assertEquals("<comboBox name=\"marcel\"/>", comboBox
031: .getDescription());
032: }
033:
034: public void testFactory() throws Exception {
035: checkFactory(new JComboBox(), ComboBox.class);
036: }
037:
038: protected UIComponent createComponent() {
039: return comboBox;
040: }
041:
042: public void testCheckContent() throws Exception {
043: assertTrue(comboBox.contentEquals(new String[] { "one", "two",
044: "three" }));
045: }
046:
047: public void testCheckContentWithErrors() throws Exception {
048: try {
049: assertTrue(comboBox.contentEquals(new String[] { "one",
050: "two", "unknown", "three" }));
051: throw new AssertionFailureNotDetectedError();
052: } catch (AssertionFailedError e) {
053: }
054: }
055:
056: public void testContains() throws Exception {
057: assertTrue(comboBox.contains("two"));
058: assertTrue(comboBox.contains(new String[] { "two", "one" }));
059: }
060:
061: public void test() throws Exception {
062: try {
063: assertTrue(comboBox.contains("unknown"));
064: throw new AssertionFailureNotDetectedError();
065: } catch (AssertionFailedError e) {
066: assertEquals(
067: "Item 'unknown' not found - actual content:[one, two, three]",
068: e.getMessage());
069: }
070:
071: try {
072: assertTrue(comboBox.contains(new String[] { "three",
073: "unknown" }));
074: throw new AssertionFailureNotDetectedError();
075: } catch (AssertionFailedError e) {
076: assertEquals(
077: "Item 'unknown' not found - actual content:[one, two, three]",
078: e.getMessage());
079: }
080: }
081:
082: public void testCheckContentWithSpecificJLabelRenderer()
083: throws Exception {
084: jComboBox.setRenderer(new DummyRenderer());
085: assertTrue(comboBox.contentEquals(new String[] { "(one)",
086: "(two)", "(three)" }));
087: }
088:
089: public void testCheckContentWithNoJLabelUsesTheModelValue()
090: throws Exception {
091: jComboBox.setRenderer(new DefaultListCellRenderer() {
092: public Component getListCellRendererComponent(JList list,
093: Object value, int index, boolean isSelected,
094: boolean cellHasFocus) {
095: return new JComboBox();
096: }
097: });
098: assertTrue(comboBox.contentEquals(new String[] { "one", "two",
099: "three" }));
100: }
101:
102: public void testUsingACustomCellRenderer() throws Exception {
103: jComboBox.setRenderer(new DefaultListCellRenderer() {
104: public Component getListCellRendererComponent(JList list,
105: Object value, int index, boolean isSelected,
106: boolean cellHasFocus) {
107: if (index == -1) {
108: return new JLabel(value.toString());
109: } else {
110: return new JTextField("level " + value);
111: }
112: }
113: });
114: comboBox.setCellValueConverter(new ListBoxCellValueConverter() {
115: public String getValue(int index,
116: Component renderedComponent, Object modelObject) {
117: if (index == -1) {
118: return ((JLabel) renderedComponent).getText();
119: } else {
120: return ((JTextField) renderedComponent).getText();
121: }
122: }
123: });
124:
125: assertTrue(comboBox.contentEquals(new String[] { "level one",
126: "level two", "level three" }));
127: comboBox.select("level two");
128: assertTrue(comboBox.selectionEquals("two"));
129: try {
130: comboBox.select("unknown");
131: throw new AssertionFailureNotDetectedError();
132: } catch (AssertionFailedError e) {
133: }
134: try {
135: assertTrue(comboBox.isEmpty("<no item>"));
136: throw new AssertionFailureNotDetectedError();
137: } catch (AssertionFailedError e) {
138: assertEquals(
139: "Unexpected content: [level one,level two,level three]",
140: e.getMessage());
141: }
142: }
143:
144: public void testAssertEmptyChecksTheDisplayedValue()
145: throws Exception {
146: jComboBox.setRenderer(new DummyRenderer());
147: jComboBox.removeAllItems();
148: assertTrue(comboBox.isEmpty("<no item>"));
149: }
150:
151: public void testAssertEmptyFailures() throws Exception {
152: try {
153: assertTrue(comboBox.isEmpty("<no item>"));
154: throw new AssertionFailureNotDetectedError();
155: } catch (AssertionFailedError e) {
156: assertEquals("Unexpected content: [one,two,three]", e
157: .getMessage());
158: }
159:
160: jComboBox.setRenderer(new DummyRenderer());
161: jComboBox.removeAllItems();
162: try {
163: assertTrue(comboBox.isEmpty("error"));
164: throw new AssertionFailureNotDetectedError();
165: } catch (AssertionFailedError e) {
166: assertEquals("expected:<error> but was:<<no item>>", e
167: .getMessage());
168: }
169: }
170:
171: public void testCheckSelectionUsesNullWhenNothingIsSelected()
172: throws Exception {
173: jComboBox.setSelectedIndex(-1);
174: comboBox.selectionEquals(null);
175: }
176:
177: public void testClickSelectsTheFirstItem() throws Exception {
178: jComboBox.setSelectedIndex(1);
179: comboBox.click();
180: assertEquals(0, jComboBox.getSelectedIndex());
181: }
182:
183: public void testClickDoesNothingIfTheComboIsEmpty()
184: throws Exception {
185: init(new JComboBox());
186: comboBox.click();
187: assertEquals(-1, jComboBox.getSelectedIndex());
188: }
189:
190: public void testBasicSelection() throws Exception {
191: comboBox.select("two");
192: assertEquals(1, jComboBox.getSelectedIndex());
193: assertTrue(comboBox.selectionEquals("two"));
194: comboBox.select("one");
195: assertEquals(0, jComboBox.getSelectedIndex());
196: assertTrue(comboBox.selectionEquals("one"));
197: }
198:
199: public void testBasicSelectionWithCustomModel() throws Exception {
200: jComboBox.setModel(new VerySimpleComboBoxModel(new String[] {
201: "one", "two", "three" }));
202: comboBox.select("two");
203: assertEquals(1, jComboBox.getSelectedIndex());
204: assertTrue(comboBox.selectionEquals("two"));
205: comboBox.select("one");
206: assertEquals(0, jComboBox.getSelectedIndex());
207: assertTrue(comboBox.selectionEquals("one"));
208: }
209:
210: public void testSelectionIsNotCaseSensitive() throws Exception {
211: comboBox.select("TwO");
212: assertEquals(1, jComboBox.getSelectedIndex());
213: assertTrue(comboBox.selectionEquals("two"));
214: comboBox.select("oNe");
215: assertEquals(0, jComboBox.getSelectedIndex());
216: assertTrue(comboBox.selectionEquals("one"));
217: }
218:
219: public void testSelectionWithSubstring() throws Exception {
220: comboBox.select("tw");
221: assertEquals(1, jComboBox.getSelectedIndex());
222: assertTrue(comboBox.selectionEquals("two"));
223: comboBox.select("ne");
224: assertEquals(0, jComboBox.getSelectedIndex());
225: assertTrue(comboBox.selectionEquals("one"));
226: }
227:
228: public void testAmbiguityInSelection() throws Exception {
229: try {
230: comboBox.select("o");
231: throw new AssertionFailureNotDetectedError();
232: } catch (ItemAmbiguityException e) {
233: assertEquals(
234: "2 items are matching the same pattern 'o': [one,two]",
235: e.getMessage());
236: }
237: }
238:
239: public void testSelectingAnUnknownValueThrowsAnException()
240: throws Exception {
241: try {
242: comboBox.select("unknown");
243: throw new AssertionFailureNotDetectedError();
244: } catch (AssertionFailedError e) {
245: }
246: }
247:
248: public void testCheckSelectedError() throws Exception {
249: try {
250: assertTrue(comboBox.selectionEquals("unknown"));
251: throw new AssertionFailureNotDetectedError();
252: } catch (AssertionFailedError e) {
253: }
254: }
255:
256: public void testAssertEditable() throws Exception {
257: jComboBox.setEditable(false);
258: assertFalse(comboBox.isEditable());
259: checkAssertionFailedError(new Functor() {
260: public void run() throws Exception {
261: assertTrue(comboBox.isEditable());
262: }
263: });
264: jComboBox.setEditable(true);
265: assertTrue(comboBox.isEditable());
266: checkAssertionFailedError(new Functor() {
267: public void run() throws Exception {
268: assertFalse(comboBox.isEditable());
269: }
270: });
271: }
272:
273: public void testSetTextIsAvailableOnlyWhenComponentIsEditable() {
274: comboBox.select("two");
275: assertTrue(comboBox.selectionEquals("two"));
276: try {
277: comboBox.setText("notInList");
278: throw new AssertionFailureNotDetectedError();
279: } catch (AssertionFailedError e) {
280: assertEquals("The combo box is not editable", e
281: .getMessage());
282: }
283: assertTrue(comboBox.selectionEquals("two"));
284:
285: jComboBox.setEditable(true);
286: comboBox.setText("notInList");
287: assertEquals("notInList", jComboBox.getSelectedItem());
288: assertTrue(comboBox.selectionEquals("notInList"));
289: assertTrue(comboBox.contentEquals(new String[] { "one", "two",
290: "three" }));
291: comboBox.select("one");
292: assertTrue(comboBox.selectionEquals("one"));
293: }
294:
295: public void testCheckSelectionUsesTheProperRendererIndex()
296: throws Exception {
297: jComboBox.setRenderer(new DefaultListCellRenderer() {
298: public Component getListCellRendererComponent(JList list,
299: Object value, int index, boolean isSelected,
300: boolean cellHasFocus) {
301: super .getListCellRendererComponent(list, value, index,
302: isSelected, cellHasFocus);
303: if (index < 0) {
304: super .setText("selected");
305: }
306: return this ;
307: }
308: });
309: assertTrue(comboBox.contentEquals(new String[] { "one", "two",
310: "three" }));
311: comboBox.select("one");
312: assertTrue(comboBox.selectionEquals("selected"));
313: comboBox.select("two");
314: assertTrue(comboBox.selectionEquals("selected"));
315: }
316:
317: public void testExceptionThrownByTheModel() throws Exception {
318: jComboBox.setModel(new DefaultComboBoxModel() {
319: public int getSize() {
320: return 1;
321: }
322:
323: public Object getElementAt(int index) {
324: throw new NullPointerException("boum");
325: }
326: });
327: checkAssertionFailedError(new Functor() {
328: public void run() throws Exception {
329: assertTrue(comboBox.contentEquals(new String[1]));
330: }
331: }, "boum");
332: }
333:
334: private static class DummyRenderer extends DefaultListCellRenderer {
335: public Component getListCellRendererComponent(JList list,
336: Object value, int index, boolean isSelected,
337: boolean cellHasFocus) {
338: String renderedValue = (index == -1) ? "<no item>" : "("
339: + (String) value + ")";
340: return super .getListCellRendererComponent(list,
341: renderedValue, index, isSelected, cellHasFocus);
342: }
343: }
344:
345: private static class VerySimpleComboBoxModel extends
346: AbstractListModel implements ComboBoxModel {
347: private final String[] content;
348: private Object selectedObject = null;
349:
350: public VerySimpleComboBoxModel(String[] content) {
351: this .content = content;
352: }
353:
354: public int getSize() {
355: return content.length;
356: }
357:
358: public Object getElementAt(int index) {
359: return content[index];
360: }
361:
362: public Object getSelectedItem() {
363: return selectedObject;
364: }
365:
366: public void setSelectedItem(Object anItem) {
367: selectedObject = anItem;
368: }
369: }
370: }
|