001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Anton Avtamonov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Component;
023: import java.awt.EventQueue;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.awt.event.ItemEvent;
027: import java.awt.event.ItemListener;
028: import java.awt.event.KeyEvent;
029: import java.util.ArrayList;
030: import java.util.List;
031: import java.util.Vector;
032: import javax.swing.event.ListDataListener;
033: import javax.swing.event.PopupMenuEvent;
034: import javax.swing.event.PopupMenuListener;
035: import javax.swing.plaf.ComboBoxUI;
036: import javax.swing.plaf.basic.BasicComboBoxEditor;
037:
038: @SuppressWarnings("serial")
039: public class JComboBoxTest extends SwingTestCase {
040: private JComboBox comboBox;
041:
042: private JFrame frame;
043:
044: public JComboBoxTest(final String name) {
045: super (name);
046: setIgnoreNotImplemented(true);
047: }
048:
049: @Override
050: protected void setUp() throws Exception {
051: comboBox = new JComboBox();
052: propertyChangeController = new PropertyChangeController();
053: comboBox.addPropertyChangeListener(propertyChangeController);
054: }
055:
056: @Override
057: protected void tearDown() throws Exception {
058: comboBox = null;
059: propertyChangeController = null;
060: if (frame != null) {
061: frame.dispose();
062: frame = null;
063: }
064: }
065:
066: public void testJComboBox() throws Exception {
067: assertNotNull(comboBox.dataModel);
068: assertEquals(comboBox.dataModel, comboBox.getModel());
069: assertTrue(comboBox.dataModel instanceof DefaultComboBoxModel);
070: DefaultComboBoxModel newModel = new DefaultComboBoxModel();
071: comboBox = new JComboBox(newModel);
072: assertEquals(newModel, comboBox.getModel());
073: comboBox = new JComboBox(new Object[] { "1", "2", "3" });
074: assertEquals(3, comboBox.getModel().getSize());
075: Vector<String> newData = new Vector<String>();
076: newData.add("1");
077: newData.add("2");
078: comboBox = new JComboBox(newData);
079: assertEquals(2, comboBox.getModel().getSize());
080: assertFalse(propertyChangeController.isChanged());
081: }
082:
083: public void testSetUI() throws Exception {
084: assertNotNull(comboBox.getUI());
085: ComboBoxUI newUI = new ComboBoxUI() {
086: @Override
087: public boolean isFocusTraversable(final JComboBox arg0) {
088: return false;
089: }
090:
091: @Override
092: public boolean isPopupVisible(final JComboBox arg0) {
093: return false;
094: }
095:
096: @Override
097: public void setPopupVisible(final JComboBox arg0,
098: final boolean arg1) {
099: }
100: };
101: comboBox.setUI(newUI);
102: assertEquals(newUI, comboBox.getUI());
103: }
104:
105: public void testGetUIClassID() throws Exception {
106: assertEquals("ComboBoxUI", comboBox.getUIClassID());
107: }
108:
109: public void testGetSetModel() throws Exception {
110: assertNotNull(comboBox.getModel());
111: DefaultComboBoxModel newModel = new DefaultComboBoxModel();
112: comboBox.setModel(newModel);
113: assertEquals(newModel, comboBox.getModel());
114: assertTrue(propertyChangeController.isChanged("model"));
115: testExceptionalCase(new ExceptionalCase() {
116: @Override
117: public void exceptionalAction() throws Exception {
118: comboBox.setModel(null);
119: }
120: });
121: }
122:
123: public void testSetIsLightWeightPopupEnabled() throws Exception {
124: assertTrue(comboBox.isLightWeightPopupEnabled());
125: comboBox.setLightWeightPopupEnabled(true);
126: assertFalse(propertyChangeController.isChanged());
127: comboBox.setLightWeightPopupEnabled(false);
128: assertFalse(comboBox.isLightWeightPopupEnabled());
129: assertTrue(propertyChangeController
130: .isChanged("lightWeightPopupEnabled"));
131: }
132:
133: public void testSetIsEditable() throws Exception {
134: assertFalse(comboBox.isEditable());
135: comboBox.setEditable(true);
136: assertTrue(comboBox.isEditable());
137: assertTrue(propertyChangeController.isChanged("editable"));
138: }
139:
140: public void testGetSetMaximumRowCount() throws Exception {
141: assertEquals(8, comboBox.getMaximumRowCount());
142: comboBox.setMaximumRowCount(-3);
143: assertEquals(-3, comboBox.getMaximumRowCount());
144: assertTrue(propertyChangeController
145: .isChanged("maximumRowCount"));
146: propertyChangeController.reset();
147: comboBox.setMaximumRowCount(5);
148: assertEquals(5, comboBox.getMaximumRowCount());
149: assertTrue(propertyChangeController
150: .isChanged("maximumRowCount"));
151: }
152:
153: public void testGetSetRenderer() throws Exception {
154: assertNotNull(comboBox.getRenderer());
155: DefaultListCellRenderer newRenderer = new DefaultListCellRenderer();
156: comboBox.setRenderer(newRenderer);
157: assertEquals(newRenderer, comboBox.getRenderer());
158: assertTrue(propertyChangeController.isChanged("renderer"));
159: }
160:
161: public void testGetSetEditor() throws Exception {
162: assertNotNull(comboBox.getEditor());
163: ComboBoxEditor newEditor = new BasicComboBoxEditor();
164: comboBox.setEditor(newEditor);
165: assertEquals(newEditor, comboBox.getEditor());
166: assertTrue(propertyChangeController.isChanged("editor"));
167: comboBox.setEditor(null);
168: assertNull(comboBox.getEditor());
169: }
170:
171: public void testSetGetSelectedItem() throws Exception {
172: ItemController itemController = new ItemController();
173: comboBox.addItemListener(itemController);
174: ActionController actionController = new ActionController();
175: comboBox.addActionListener(actionController);
176: assertNull(comboBox.getSelectedItem());
177: comboBox.setSelectedItem("a");
178: assertNull(comboBox.getSelectedItem());
179: assertEquals(-1, comboBox.getSelectedIndex());
180: assertNull(actionController.getEvent());
181: assertTrue(itemController.getEvents().isEmpty());
182: actionController.reset();
183: itemController.reset();
184: comboBox.setEditable(true);
185: comboBox.setSelectedItem("a");
186: assertEquals("a", comboBox.getSelectedItem());
187: assertEquals(-1, comboBox.getSelectedIndex());
188: assertEquals(1, itemController.getEvents().size());
189: assertEquals(ItemEvent.SELECTED, itemController.getEvents()
190: .get(0).getStateChange());
191: assertEquals(ItemEvent.ITEM_STATE_CHANGED, itemController
192: .getEvents().get(0).getID());
193: assertNotNull(actionController.getEvent());
194: actionController.reset();
195: itemController.reset();
196: comboBox.setSelectedItem("b");
197: assertEquals("b", comboBox.getSelectedItem());
198: assertEquals(-1, comboBox.getSelectedIndex());
199: assertEquals(2, itemController.getEvents().size());
200: assertEquals(ItemEvent.DESELECTED, itemController.getEvents()
201: .get(0).getStateChange());
202: assertEquals(ItemEvent.ITEM_STATE_CHANGED, itemController
203: .getEvents().get(0).getID());
204: assertEquals(ItemEvent.SELECTED, itemController.getEvents()
205: .get(1).getStateChange());
206: assertEquals(ItemEvent.ITEM_STATE_CHANGED, itemController
207: .getEvents().get(1).getID());
208: assertNotNull(actionController.getEvent());
209: actionController.reset();
210: itemController.reset();
211: assertEquals("b", comboBox.getSelectedItem());
212: comboBox.setSelectedItem("b");
213: assertTrue(itemController.getEvents().isEmpty());
214: if (isHarmony()) {
215: assertNull(actionController.getEvent());
216: } else {
217: assertNotNull(actionController.getEvent());
218: }
219: actionController.reset();
220: itemController.reset();
221: comboBox.setEditable(false);
222: comboBox.addItem("a");
223: comboBox.addItem("b");
224: assertEquals(1, comboBox.getSelectedIndex());
225: comboBox.setSelectedItem("c");
226: assertEquals("b", comboBox.getSelectedItem());
227: assertEquals(1, comboBox.getSelectedIndex());
228: assertNull(actionController.getEvent());
229: assertTrue(itemController.getEvents().isEmpty());
230: assertEquals(1, comboBox.getSelectedIndex());
231: comboBox.setSelectedItem("b");
232: assertEquals(1, comboBox.getSelectedIndex());
233: if (isHarmony()) {
234: assertNull(actionController.getEvent());
235: } else {
236: assertNotNull(actionController.getEvent());
237: }
238: assertTrue(itemController.getEvents().isEmpty());
239: comboBox.setSelectedItem("a");
240: assertEquals("a", comboBox.getSelectedItem());
241: assertEquals(0, comboBox.getSelectedIndex());
242: assertNotNull(actionController.getEvent());
243: assertEquals(2, itemController.getEvents().size());
244: comboBox.removeItem("a");
245: assertEquals("b", comboBox.getSelectedItem());
246: assertEquals(0, comboBox.getSelectedIndex());
247: }
248:
249: public void testGetSetSelectedIndex() throws Exception {
250: assertEquals(-1, comboBox.getSelectedIndex());
251: testExceptionalCase(new IllegalArgumentCase() {
252: @Override
253: public void exceptionalAction() throws Exception {
254: comboBox.setSelectedIndex(0);
255: }
256: });
257: testExceptionalCase(new IllegalArgumentCase() {
258: @Override
259: public void exceptionalAction() throws Exception {
260: comboBox.setSelectedIndex(-2);
261: }
262: });
263: comboBox.setSelectedIndex(-1);
264: assertEquals(-1, comboBox.getSelectedIndex());
265: comboBox.addItem("a");
266: comboBox.addItem("b");
267: assertEquals(0, comboBox.getSelectedIndex());
268: assertEquals("a", comboBox.getSelectedItem());
269: comboBox.setSelectedIndex(0);
270: assertEquals(0, comboBox.getSelectedIndex());
271: assertEquals("a", comboBox.getSelectedItem());
272: comboBox.removeItem("a");
273: assertEquals(0, comboBox.getSelectedIndex());
274: assertEquals("b", comboBox.getSelectedItem());
275: comboBox.addItem("c");
276: comboBox.addItem("d");
277: comboBox.addItem("e");
278: comboBox.setSelectedItem("d");
279: assertEquals(2, comboBox.getSelectedIndex());
280: comboBox.removeItem("d");
281: assertEquals(1, comboBox.getSelectedIndex());
282: assertEquals("c", comboBox.getSelectedItem());
283: comboBox.setEditable(true);
284: comboBox.setSelectedItem("f");
285: assertEquals(-1, comboBox.getSelectedIndex());
286: }
287:
288: public void testPrototypeDisplayValue() throws Exception {
289: assertNull(comboBox.getPrototypeDisplayValue());
290: comboBox.setPrototypeDisplayValue("a");
291: assertEquals("a", comboBox.getPrototypeDisplayValue());
292: assertTrue(propertyChangeController
293: .isChanged("prototypeDisplayValue"));
294: }
295:
296: public void testAddItem() throws Exception {
297: ItemController itemController = new ItemController();
298: comboBox.addItemListener(itemController);
299: ActionController actionController = new ActionController();
300: comboBox.addActionListener(actionController);
301: assertNull(comboBox.getSelectedItem());
302: assertEquals(-1, comboBox.getSelectedIndex());
303: comboBox.addItem("a");
304: assertEquals(1, comboBox.getModel().getSize());
305: assertEquals(1, itemController.getEvents().size());
306: assertNotNull(actionController.getEvent());
307: assertEquals("a", comboBox.getSelectedItem());
308: assertEquals(0, comboBox.getSelectedIndex());
309: itemController.reset();
310: actionController.reset();
311: comboBox.addItem("b");
312: assertEquals(0, itemController.getEvents().size());
313: assertNull(actionController.getEvent());
314: ComboBoxModel immutableModel = new ComboBoxModel() {
315: public Object getSelectedItem() {
316: return null;
317: }
318:
319: public void setSelectedItem(final Object value) {
320: }
321:
322: public void addListDataListener(final ListDataListener l) {
323: }
324:
325: public Object getElementAt(final int index) {
326: return null;
327: }
328:
329: public int getSize() {
330: return 0;
331: }
332:
333: public void removeListDataListener(final ListDataListener l) {
334: }
335: };
336: comboBox.setModel(immutableModel);
337: testExceptionalCase(new ExceptionalCase() {
338: @Override
339: public void exceptionalAction() throws Exception {
340: comboBox.addItem("a");
341: }
342: });
343: }
344:
345: public void testInsertItemAt() throws Exception {
346: ItemController itemController = new ItemController();
347: comboBox.addItemListener(itemController);
348: ActionController actionController = new ActionController();
349: comboBox.addActionListener(actionController);
350: assertNull(comboBox.getSelectedItem());
351: assertEquals(-1, comboBox.getSelectedIndex());
352: comboBox.insertItemAt("a", 0);
353: assertEquals(1, comboBox.getModel().getSize());
354: assertEquals(0, itemController.getEvents().size());
355: assertNull(actionController.getEvent());
356: assertNull(comboBox.getSelectedItem());
357: assertEquals(-1, comboBox.getSelectedIndex());
358: itemController.reset();
359: actionController.reset();
360: comboBox.insertItemAt("b", 1);
361: assertEquals(0, itemController.getEvents().size());
362: assertNull(actionController.getEvent());
363: ComboBoxModel immutableModel = new ComboBoxModel() {
364: public Object getSelectedItem() {
365: return null;
366: }
367:
368: public void setSelectedItem(final Object value) {
369: }
370:
371: public void addListDataListener(final ListDataListener l) {
372: }
373:
374: public Object getElementAt(final int index) {
375: return null;
376: }
377:
378: public int getSize() {
379: return 0;
380: }
381:
382: public void removeListDataListener(final ListDataListener l) {
383: }
384: };
385: comboBox.setModel(immutableModel);
386: testExceptionalCase(new ExceptionalCase() {
387: @Override
388: public void exceptionalAction() throws Exception {
389: comboBox.insertItemAt("c", 0);
390: }
391: });
392: }
393:
394: public void testRemoveItem() throws Exception {
395: ItemController itemController = new ItemController();
396: comboBox.addItemListener(itemController);
397: ActionController actionController = new ActionController();
398: comboBox.addActionListener(actionController);
399: comboBox.addItem("a");
400: comboBox.addItem("b");
401: assertEquals("a", comboBox.getSelectedItem());
402: itemController.reset();
403: actionController.reset();
404: comboBox.removeItem("a");
405: assertEquals("b", comboBox.getSelectedItem());
406: assertEquals(1, comboBox.getModel().getSize());
407: assertEquals(2, itemController.getEvents().size());
408: assertNotNull(actionController.getEvent());
409: itemController.reset();
410: actionController.reset();
411: comboBox.removeItem("a");
412: assertEquals(1, comboBox.getModel().getSize());
413: assertEquals(0, itemController.getEvents().size());
414: assertNull(actionController.getEvent());
415: }
416:
417: public void testRemoveItemAt() throws Exception {
418: ItemController itemController = new ItemController();
419: comboBox.addItemListener(itemController);
420: ActionController actionController = new ActionController();
421: comboBox.addActionListener(actionController);
422: comboBox.addItem("a");
423: comboBox.addItem("b");
424: assertEquals("a", comboBox.getSelectedItem());
425: itemController.reset();
426: actionController.reset();
427: comboBox.removeItemAt(0);
428: assertEquals("b", comboBox.getSelectedItem());
429: assertEquals(1, comboBox.getModel().getSize());
430: assertEquals(2, itemController.getEvents().size());
431: assertNotNull(actionController.getEvent());
432: itemController.reset();
433: actionController.reset();
434: comboBox.removeItemAt(0);
435: assertEquals(0, comboBox.getModel().getSize());
436: assertNull(comboBox.getSelectedItem());
437: assertEquals(1, itemController.getEvents().size());
438: assertNotNull(actionController.getEvent());
439: testExceptionalCase(new ExceptionalCase() {
440: @Override
441: public void exceptionalAction() throws Exception {
442: comboBox.removeItemAt(0);
443: }
444: });
445: }
446:
447: public void testGetItemCount() throws Exception {
448: assertEquals(0, comboBox.getItemCount());
449: comboBox.addItem("a");
450: assertEquals(1, comboBox.getItemCount());
451: comboBox.addItem("b");
452: assertEquals(2, comboBox.getItemCount());
453: comboBox.removeItem("b");
454: assertEquals(1, comboBox.getItemCount());
455: }
456:
457: public void testGetItemAt() throws Exception {
458: assertNull(comboBox.getItemAt(0));
459: assertNull(comboBox.getItemAt(-1));
460: comboBox.addItem("a");
461: comboBox.addItem("b");
462: assertEquals("a", comboBox.getItemAt(0));
463: assertEquals("b", comboBox.getItemAt(1));
464: assertNull(comboBox.getItemAt(2));
465: }
466:
467: public void testAddRemoveGetFireItemListener() throws Exception {
468: comboBox.getUI().uninstallUI(comboBox);
469: assertEquals(0, comboBox.getItemListeners().length);
470: ItemController l = new ItemController();
471: comboBox.addItemListener(l);
472: assertEquals(1, comboBox.getItemListeners().length);
473: comboBox.addItemListener(new ItemController());
474: assertEquals(2, comboBox.getItemListeners().length);
475: comboBox.fireItemStateChanged(new ItemEvent(comboBox,
476: ItemEvent.ITEM_STATE_CHANGED, "a", ItemEvent.SELECTED));
477: assertEquals(1, l.getEvents().size());
478: assertEquals(comboBox, l.getEvents().get(0).getSource());
479: comboBox.removeItemListener(l);
480: assertEquals(1, comboBox.getItemListeners().length);
481: }
482:
483: public void testAddRemoveGetFireActionListener() throws Exception {
484: comboBox.getUI().uninstallUI(comboBox);
485: assertEquals(0, comboBox.getItemListeners().length);
486: ActionController l = new ActionController();
487: comboBox.addActionListener(l);
488: assertEquals(1, comboBox.getActionListeners().length);
489: comboBox.addActionListener(new ActionController());
490: assertEquals(2, comboBox.getActionListeners().length);
491: comboBox.fireActionEvent();
492: assertNotNull(l.getEvent());
493: assertEquals(comboBox, l.getEvent().getSource());
494: comboBox.removeActionListener(l);
495: assertEquals(1, comboBox.getActionListeners().length);
496: }
497:
498: public void testAddRemoveGetFirePopupListener() throws Exception {
499: comboBox.getUI().uninstallUI(comboBox);
500: assertEquals(0, comboBox.getItemListeners().length);
501: PopupMenuController l = new PopupMenuController();
502: comboBox.addPopupMenuListener(l);
503: assertEquals(1, comboBox.getPopupMenuListeners().length);
504: comboBox.addPopupMenuListener(new PopupMenuController());
505: assertEquals(2, comboBox.getPopupMenuListeners().length);
506: comboBox.firePopupMenuCanceled();
507: assertNotNull(l.getEvent());
508: assertEquals(PopupMenuController.CANCELLED, l.getEventType());
509: l.reset();
510: comboBox.firePopupMenuWillBecomeVisible();
511: assertNotNull(l.getEvent());
512: assertEquals(PopupMenuController.VISIBLE, l.getEventType());
513: assertEquals(comboBox, l.getEvent().getSource());
514: l.reset();
515: comboBox.firePopupMenuWillBecomeInvisible();
516: assertNotNull(l.getEvent());
517: assertEquals(PopupMenuController.INVISIBLE, l.getEventType());
518: comboBox.removePopupMenuListener(l);
519: assertEquals(1, comboBox.getPopupMenuListeners().length);
520: }
521:
522: public void testGetSetActionCommand() throws Exception {
523: assertEquals("comboBoxChanged", comboBox.getActionCommand());
524: comboBox.setActionCommand("anotherCommand");
525: assertEquals("anotherCommand", comboBox.getActionCommand());
526: assertFalse(propertyChangeController.isChanged());
527: ActionController actionController = new ActionController();
528: comboBox.addActionListener(actionController);
529: comboBox.addItem("any");
530: assertEquals("anotherCommand", actionController.getEvent()
531: .getActionCommand());
532: }
533:
534: public void testGetSelectedObjects() throws Exception {
535: assertNull(comboBox.getSelectedItem());
536: assertEquals(0, comboBox.getSelectedObjects().length);
537: comboBox.setEditable(true);
538: comboBox.setSelectedItem("a");
539: assertEquals("a", comboBox.getSelectedObjects()[0]);
540: }
541:
542: public void testSetEnabled() throws Exception {
543: assertTrue(comboBox.isEnabled());
544: comboBox.setEnabled(false);
545: assertFalse(comboBox.isEnabled());
546: assertTrue(propertyChangeController.isChanged("enabled"));
547: }
548:
549: public void testSetGetAction() throws Exception {
550: assertEquals(0, comboBox.getActionListeners().length);
551: assertNull(comboBox.getAction());
552: TestAction action = new TestAction();
553: comboBox.setAction(action);
554: assertTrue(propertyChangeController.isChanged("action"));
555: assertEquals(action, comboBox.getAction());
556: assertEquals(1, comboBox.getActionListeners().length);
557: propertyChangeController.reset();
558: comboBox.setAction(action);
559: assertFalse(propertyChangeController.isChanged());
560: assertEquals(1, comboBox.getActionListeners().length);
561: action.reset();
562: comboBox.fireActionEvent();
563: assertEquals(1, action.getEvents().size());
564: action.reset();
565: comboBox.addActionListener(action);
566: comboBox.fireActionEvent();
567: assertEquals(2, action.getEvents().size());
568: assertEquals(2, comboBox.getActionListeners().length);
569: action.reset();
570: comboBox.setAction(null);
571: assertNull(comboBox.getAction());
572: comboBox.fireActionEvent();
573: assertEquals(1, action.getEvents().size());
574: assertEquals(1, comboBox.getActionListeners().length);
575: action.reset();
576: comboBox.setAction(action);
577: comboBox.fireActionEvent();
578: assertEquals(1, action.getEvents().size());
579: assertEquals(1, comboBox.getActionListeners().length);
580: }
581:
582: public void testIsSetPopupVisible() throws Exception {
583: createVisibleComboBox();
584: assertFalse(comboBox.isPopupVisible());
585: assertFalse(comboBox.getUI().isPopupVisible(comboBox));
586: PopupMenuController pmc = new PopupMenuController();
587: comboBox.addPopupMenuListener(pmc);
588: comboBox.setPopupVisible(true);
589: assertTrue(comboBox.isPopupVisible());
590: assertTrue(comboBox.getUI().isPopupVisible(comboBox));
591: assertNotNull(pmc.getEvent());
592: assertEquals(PopupMenuController.VISIBLE, pmc.getEventType());
593: pmc.reset();
594: comboBox.getUI().setPopupVisible(comboBox, false);
595: assertFalse(comboBox.isPopupVisible());
596: assertNotNull(pmc.getEvent());
597: assertEquals(PopupMenuController.INVISIBLE, pmc.getEventType());
598: }
599:
600: public void testShowHidePopup() throws Exception {
601: createVisibleComboBox();
602: assertFalse(comboBox.isPopupVisible());
603: comboBox.showPopup();
604: assertTrue(comboBox.isPopupVisible());
605: comboBox.hidePopup();
606: assertFalse(comboBox.isPopupVisible());
607: }
608:
609: public void testCreateDefaultKeySelectionManager() throws Exception {
610: JComboBox.KeySelectionManager ksm = comboBox
611: .createDefaultKeySelectionManager();
612: assertNotNull(ksm);
613: comboBox.setKeySelectionManager(null);
614: comboBox.selectWithKeyChar('a');
615: assertNotNull(comboBox.getKeySelectionManager());
616: }
617:
618: public void testDefaultKeySelectionManager() throws Exception {
619: JComboBox.KeySelectionManager ksm = comboBox
620: .createDefaultKeySelectionManager();
621: DefaultComboBoxModel model = new DefaultComboBoxModel();
622: assertEquals(-1, ksm.selectionForKey('a', model));
623: model.addElement("a 0");
624: model.addElement("b 0");
625: model.addElement(" b 0");
626: assertEquals(0, ksm.selectionForKey('a', model));
627: assertEquals(1, ksm.selectionForKey('b', model));
628: assertEquals(2, ksm.selectionForKey(' ', model));
629: }
630:
631: @SuppressWarnings("deprecation")
632: public void testProcessKeyEvent() throws Exception {
633: comboBox.setKeySelectionManager(null);
634: createVisibleComboBox();
635: PopupMenuController pmc = new PopupMenuController();
636: comboBox.addPopupMenuListener(pmc);
637: KeyEvent event = new KeyEvent(comboBox, KeyEvent.KEY_PRESSED,
638: EventQueue.getMostRecentEventTime(), 0, KeyEvent.VK_A);
639: comboBox.processKeyEvent(event);
640: assertNull(pmc.getEvent());
641: event = new KeyEvent(comboBox, KeyEvent.KEY_PRESSED, EventQueue
642: .getMostRecentEventTime(), 0, KeyEvent.VK_TAB);
643: comboBox.processKeyEvent(event);
644: assertNull(pmc.getEvent());
645: comboBox.setPopupVisible(true);
646: assertNotNull(pmc.getEvent());
647: assertEquals(PopupMenuController.VISIBLE, pmc.getEventType());
648: pmc.reset();
649: comboBox.addItem("a");
650: comboBox.addItem("b");
651: event = new KeyEvent(comboBox, KeyEvent.KEY_PRESSED, EventQueue
652: .getMostRecentEventTime(), 0, KeyEvent.VK_A);
653: comboBox.processKeyEvent(event);
654: assertNull(pmc.getEvent());
655: assertEquals("a", comboBox.getSelectedItem());
656: pmc.reset();
657: event = new KeyEvent(comboBox, KeyEvent.KEY_PRESSED, EventQueue
658: .getMostRecentEventTime(), 0, KeyEvent.VK_B);
659: comboBox.processKeyEvent(event);
660: assertNull(pmc.getEvent());
661: assertEquals("b", comboBox.getSelectedItem());
662: pmc.reset();
663: event = new KeyEvent(comboBox, KeyEvent.KEY_PRESSED, EventQueue
664: .getMostRecentEventTime(), 0, KeyEvent.VK_A);
665: comboBox.processKeyEvent(event);
666: assertNull(pmc.getEvent());
667: assertEquals("a", comboBox.getSelectedItem());
668: pmc.reset();
669: event = new KeyEvent(comboBox, KeyEvent.KEY_PRESSED, EventQueue
670: .getMostRecentEventTime(), 0, KeyEvent.VK_TAB);
671: comboBox.processKeyEvent(event);
672: assertNotNull(pmc.getEvent());
673: assertEquals(PopupMenuController.INVISIBLE, pmc.getEventType());
674: assertEquals("a", comboBox.getSelectedItem());
675: comboBox
676: .setKeySelectionManager(new JComboBox.KeySelectionManager() {
677: public int selectionForKey(final char key,
678: final ComboBoxModel model) {
679: return -1;
680: }
681: });
682: pmc.reset();
683: event = new KeyEvent(comboBox, KeyEvent.KEY_PRESSED, EventQueue
684: .getMostRecentEventTime(), 0, KeyEvent.VK_TAB);
685: comboBox.processKeyEvent(event);
686: assertNull(pmc.getEvent());
687: assertEquals("a", comboBox.getSelectedItem());
688: pmc.reset();
689: event = new KeyEvent(comboBox, KeyEvent.KEY_PRESSED, EventQueue
690: .getMostRecentEventTime(), 0, KeyEvent.VK_A);
691: comboBox.processKeyEvent(event);
692: assertNull(pmc.getEvent());
693: assertEquals("a", comboBox.getSelectedItem());
694: pmc.reset();
695: event = new KeyEvent(comboBox, KeyEvent.KEY_PRESSED, EventQueue
696: .getMostRecentEventTime(), 0, KeyEvent.VK_B);
697: comboBox.processKeyEvent(event);
698: assertNull(pmc.getEvent());
699: assertEquals("a", comboBox.getSelectedItem());
700: }
701:
702: public void testSelectWithKeyChar() throws Exception {
703: comboBox.setKeySelectionManager(null);
704: PopupMenuController pmc = new PopupMenuController();
705: comboBox.addPopupMenuListener(pmc);
706: assertFalse(comboBox.selectWithKeyChar('a'));
707: assertNull(pmc.getEvent());
708: comboBox.addItem("a1");
709: comboBox.addItem("a2");
710: comboBox.addItem("a3");
711: comboBox.addItem("b1");
712: assertEquals("a1", comboBox.getSelectedItem());
713: assertFalse(comboBox.selectWithKeyChar('c'));
714: assertTrue(comboBox.selectWithKeyChar('A'));
715: assertEquals("a2", comboBox.getSelectedItem());
716: assertNull(pmc.getEvent());
717: assertTrue(comboBox.selectWithKeyChar('a'));
718: assertEquals("a3", comboBox.getSelectedItem());
719: assertNull(pmc.getEvent());
720: assertTrue(comboBox.selectWithKeyChar('A'));
721: assertEquals("a1", comboBox.getSelectedItem());
722: assertNull(pmc.getEvent());
723: assertTrue(comboBox.selectWithKeyChar('b'));
724: assertEquals("b1", comboBox.getSelectedItem());
725: assertNull(pmc.getEvent());
726: assertTrue(comboBox.selectWithKeyChar('b'));
727: assertEquals("b1", comboBox.getSelectedItem());
728: assertNull(pmc.getEvent());
729: comboBox
730: .setKeySelectionManager(new JComboBox.KeySelectionManager() {
731: public int selectionForKey(final char key,
732: final ComboBoxModel model) {
733: return -1;
734: }
735: });
736: assertFalse(comboBox.selectWithKeyChar('a'));
737: assertEquals("b1", comboBox.getSelectedItem());
738: assertFalse(comboBox.selectWithKeyChar('b'));
739: assertEquals("b1", comboBox.getSelectedItem());
740: comboBox
741: .setKeySelectionManager(new JComboBox.KeySelectionManager() {
742: public int selectionForKey(final char key,
743: final ComboBoxModel model) {
744: return 1;
745: }
746: });
747: assertTrue(comboBox.selectWithKeyChar('a'));
748: assertEquals("a2", comboBox.getSelectedItem());
749: assertTrue(comboBox.selectWithKeyChar('b'));
750: assertEquals("a2", comboBox.getSelectedItem());
751: assertTrue(comboBox.selectWithKeyChar('c'));
752: assertEquals("a2", comboBox.getSelectedItem());
753: }
754:
755: public void testGetSetKeySelectionManager() throws Exception {
756: assertNotNull(comboBox.getKeySelectionManager());
757: JComboBox.KeySelectionManager manager = new JComboBox.KeySelectionManager() {
758: public int selectionForKey(final char key,
759: final ComboBoxModel model) {
760: return 0;
761: }
762: };
763: comboBox.setKeySelectionManager(manager);
764: assertEquals(manager, comboBox.getKeySelectionManager());
765: assertFalse(propertyChangeController.isChanged());
766: }
767:
768: public void testCreateActionPropertyChangeListener()
769: throws Exception {
770: Action action1 = new AbstractAction() {
771: public void actionPerformed(final ActionEvent e) {
772: }
773: };
774: comboBox.setAction(action1);
775: assertTrue(comboBox.isEnabled());
776: action1.setEnabled(false);
777: assertFalse(comboBox.isEnabled());
778: action1.setEnabled(true);
779: assertTrue(comboBox.isEnabled());
780: Action action2 = new AbstractAction() {
781: public void actionPerformed(final ActionEvent e) {
782: }
783: };
784: comboBox.setAction(action2);
785: action1.setEnabled(false);
786: assertTrue(comboBox.isEnabled());
787: action2.setEnabled(false);
788: assertFalse(comboBox.isEnabled());
789: action2.setEnabled(true);
790: assertTrue(comboBox.isEnabled());
791: comboBox.setAction(null);
792: assertTrue(comboBox.isEnabled());
793: action2.setEnabled(false);
794: assertTrue(comboBox.isEnabled());
795: }
796:
797: public void testConfigurePropertiesFromAction() throws Exception {
798: comboBox.setToolTipText("combo tooltip");
799: comboBox.setEnabled(false);
800: assertEquals("combo tooltip", comboBox.getToolTipText());
801: assertFalse(comboBox.isEnabled());
802: Action action = new AbstractAction() {
803: public void actionPerformed(final ActionEvent e) {
804: }
805: };
806: action.putValue(Action.SHORT_DESCRIPTION, "action tooltip");
807: comboBox.setAction(action);
808: assertEquals("action tooltip", comboBox.getToolTipText());
809: assertTrue(comboBox.isEnabled());
810: comboBox.setAction(null);
811: assertNull(comboBox.getToolTipText());
812: assertTrue(comboBox.isEnabled());
813: }
814:
815: public void testInstallAncestorListener() throws Exception {
816: assertEquals(1, comboBox.getAncestorListeners().length);
817: }
818:
819: public void testHarmony5223() {
820: ComboBoxEditor editor = new NullComboBoxEditor();
821: comboBox.setEditor(editor);
822: assertEquals(editor, comboBox.getEditor());
823: }
824:
825: public class NullComboBoxEditor extends BasicComboBoxEditor {
826: public NullComboBoxEditor() {
827: super ();
828: }
829:
830: public Component getEditorComponent() {
831: return null;
832: }
833: }
834:
835: private class ActionController implements ActionListener {
836: private ActionEvent event;
837:
838: public void actionPerformed(final ActionEvent e) {
839: event = e;
840: }
841:
842: public void reset() {
843: event = null;
844: }
845:
846: public ActionEvent getEvent() {
847: return event;
848: }
849: }
850:
851: private class TestAction extends AbstractAction {
852: private List<ActionEvent> events = new ArrayList<ActionEvent>();
853:
854: public void actionPerformed(final ActionEvent e) {
855: events.add(e);
856: }
857:
858: public void reset() {
859: events.clear();
860: }
861:
862: public List<ActionEvent> getEvents() {
863: return events;
864: }
865: }
866:
867: private class ItemController implements ItemListener {
868: private List<ItemEvent> eventList = new ArrayList<ItemEvent>();
869:
870: public void itemStateChanged(final ItemEvent e) {
871: eventList.add(e);
872: }
873:
874: public void reset() {
875: eventList.clear();
876: }
877:
878: public List<ItemEvent> getEvents() {
879: return eventList;
880: }
881: }
882:
883: private static class PopupMenuController implements
884: PopupMenuListener {
885: public static final int CANCELLED = 0;
886:
887: public static final int VISIBLE = 1;
888:
889: public static final int INVISIBLE = 2;
890:
891: private PopupMenuEvent event;
892:
893: private int eventType = -1;
894:
895: public void reset() {
896: event = null;
897: eventType = -1;
898: }
899:
900: public PopupMenuEvent getEvent() {
901: return event;
902: }
903:
904: public int getEventType() {
905: return eventType;
906: }
907:
908: public void popupMenuCanceled(final PopupMenuEvent e) {
909: event = e;
910: eventType = CANCELLED;
911: }
912:
913: public void popupMenuWillBecomeInvisible(final PopupMenuEvent e) {
914: event = e;
915: eventType = INVISIBLE;
916: }
917:
918: public void popupMenuWillBecomeVisible(final PopupMenuEvent e) {
919: event = e;
920: eventType = VISIBLE;
921: }
922: }
923:
924: @SuppressWarnings("deprecation")
925: private void createVisibleComboBox() {
926: frame = new JFrame();
927: frame.getContentPane().add(comboBox);
928: frame.show();
929: }
930: }
|