01: package abbot.tester;
02:
03: import java.awt.event.*;
04: import java.util.Vector;
05:
06: import javax.swing.JComboBox;
07:
08: import junit.extensions.abbot.*;
09:
10: /** Unit test to verify supported JComboBox operations.<p> */
11:
12: public class JComboBoxTesterTest extends ComponentTestFixture {
13:
14: private JComboBoxTester tester;
15: private JComboBox box;
16: private Vector list;
17: private static final int MAX_ENTRIES = 100;
18:
19: /** Create a new test case with the given name. */
20: public JComboBoxTesterTest(String name) {
21: super (name);
22: }
23:
24: private volatile String selectedItem;
25: private volatile int selectedIndex;
26:
27: protected void setUp() {
28: tester = (JComboBoxTester) ComponentTester
29: .getTester(JComboBox.class);
30: list = new Vector();
31: for (int i = 0; i < MAX_ENTRIES; i++) {
32: list.add("item " + i);
33: }
34: box = new JComboBox(list);
35: box.setSelectedIndex(-1);
36: box.addActionListener(new ActionListener() {
37: public void actionPerformed(ActionEvent ev) {
38: JComboBox cb = (JComboBox) ev.getSource();
39: selectedItem = (String) cb.getSelectedItem();
40: selectedIndex = cb.getSelectedIndex();
41: }
42: });
43: }
44:
45: // FIXME: occasionally see failures on OSX
46: // (selected index = -1 @ 50, -1 @ 80)
47: public void testSelectIndex() {
48: showFrame(box);
49: for (int i = 0; i < MAX_ENTRIES; i += MAX_ENTRIES / 10) {
50: selectedIndex = -1;
51: tester.actionSelectIndex(box, i);
52: assertEquals("Wrong index selected", i, selectedIndex);
53: }
54: }
55:
56: public void testSelectItem() {
57: showFrame(box);
58: for (int i = 0; i < MAX_ENTRIES; i += MAX_ENTRIES / 10) {
59: selectedItem = null;
60: String item = "item " + i;
61: tester.actionSelectItem(box, item);
62: assertEquals("Wrong item selected", item, selectedItem);
63: }
64: }
65:
66: public void testSelectIndexEditable() {
67: box.setEditable(true);
68: showFrame(box);
69: for (int i = 0; i < MAX_ENTRIES; i += MAX_ENTRIES / 10) {
70: selectedIndex = -1;
71: tester.actionSelectIndex(box, i);
72: assertEquals("Wrong index selected (editable)", i,
73: selectedIndex);
74: }
75: }
76:
77: public void testSelectItemEditable() {
78: box.setEditable(true);
79: showFrame(box);
80: for (int i = 0; i < MAX_ENTRIES; i += MAX_ENTRIES / 10) {
81: selectedItem = null;
82: String item = "item " + i;
83: tester.actionSelectItem(box, item);
84: assertEquals("Wrong item selected (editable)", item,
85: selectedItem);
86: }
87: }
88:
89: public static void main(String[] args) {
90: RepeatHelper.runTests(args, JComboBoxTesterTest.class);
91: }
92: }
|