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.plaf.basic;
021:
022: import java.awt.Dimension;
023: import java.awt.Font;
024: import java.awt.Insets;
025: import java.awt.Rectangle;
026: import javax.swing.BorderFactory;
027: import javax.swing.CellRendererPane;
028: import javax.swing.DefaultListCellRenderer;
029: import javax.swing.JButton;
030: import javax.swing.JComboBox;
031: import javax.swing.JComponent;
032: import javax.swing.JFrame;
033: import javax.swing.JLabel;
034: import javax.swing.JTextField;
035: import javax.swing.ListCellRenderer;
036: import javax.swing.SwingConstants;
037: import javax.swing.SwingTestCase;
038: import javax.swing.plaf.ComponentUI;
039:
040: public class BasicComboBoxUITest extends SwingTestCase {
041: private BasicComboBoxUI ui;
042:
043: private JComboBox comboBox;
044:
045: private JFrame frame;
046:
047: public BasicComboBoxUITest(final String name) {
048: super (name);
049: }
050:
051: @Override
052: protected void setUp() throws Exception {
053: ui = new BasicComboBoxUI();
054: comboBox = new JComboBox();
055: frame = new JFrame();
056: }
057:
058: @Override
059: protected void tearDown() throws Exception {
060: ui = null;
061: comboBox = null;
062: frame.dispose();
063: }
064:
065: public void testBasicComboBoxUI() throws Exception {
066: assertNotNull(ui.currentValuePane);
067: assertFalse(ui.currentValuePane.isVisible());
068: }
069:
070: public void testCreateUI() throws Exception {
071: ComponentUI ui1 = BasicComboBoxUI.createUI(new JComboBox());
072: assertNotNull(ui1);
073: ComponentUI ui2 = BasicComboBoxUI.createUI(new JComboBox());
074: assertNotNull(ui2);
075: assertNotSame(ui1, ui2);
076: }
077:
078: public void testCreatePopup() throws Exception {
079: assertNull(ui.popup);
080: ui.comboBox = comboBox;
081: assertNotSame(ui.createPopup(), ui.createPopup());
082: assertNull(ui.popup);
083: }
084:
085: public void testCreateKeyListener() throws Exception {
086: if (isHarmony()) {
087: assertTrue(ui.createKeyListener() instanceof BasicComboBoxUI.KeyHandler);
088: }
089: }
090:
091: public void testCreateFocusListener() throws Exception {
092: if (isHarmony()) {
093: assertTrue(ui.createFocusListener() instanceof BasicComboBoxUI.FocusHandler);
094: }
095: }
096:
097: public void testCreateListDataListener() throws Exception {
098: if (isHarmony()) {
099: assertTrue(ui.createListDataListener() instanceof BasicComboBoxUI.ListDataHandler);
100: }
101: }
102:
103: public void testCreateItemListener() throws Exception {
104: assertNull(ui.createItemListener());
105: }
106:
107: public void testCreatePropertyChangeListener() throws Exception {
108: if (isHarmony()) {
109: assertTrue(ui.createPropertyChangeListener() instanceof BasicComboBoxUI.PropertyChangeHandler);
110: }
111: }
112:
113: public void testCreateLayoutManager() throws Exception {
114: if (isHarmony()) {
115: assertTrue(ui.createLayoutManager() instanceof BasicComboBoxUI.ComboBoxLayoutManager);
116: }
117: }
118:
119: public void testCreateRenderer() throws Exception {
120: assertTrue(ui.createRenderer() instanceof BasicComboBoxRenderer.UIResource);
121: }
122:
123: public void testCreateEditor() throws Exception {
124: assertTrue(ui.createEditor().getClass() == BasicComboBoxEditor.UIResource.class);
125: }
126:
127: public void testAddRemoveEditor() throws Exception {
128: ui.comboBox = comboBox;
129: ui.uninstallComponents();
130: assertEquals(0, ui.comboBox.getComponentCount());
131: assertNull(ui.editor);
132: ui.addEditor();
133: assertEquals(1, ui.comboBox.getComponentCount());
134: assertNotNull(ui.editor);
135: assertTrue(ui.editor instanceof JTextField);
136: ui.removeEditor();
137: assertNull(ui.editor);
138: assertEquals(0, ui.comboBox.getComponentCount());
139: }
140:
141: public void testCreateArrowButton() throws Exception {
142: JButton arrowButton = ui.createArrowButton();
143: assertTrue(arrowButton instanceof BasicArrowButton);
144: assertEquals(SwingConstants.SOUTH,
145: ((BasicArrowButton) arrowButton).getDirection());
146: assertEquals("", arrowButton.getText());
147: }
148:
149: public void testIsPopupVisible() throws Exception {
150: createVisibleCombo();
151: ui.popup = new BasicComboPopup(comboBox);
152: assertFalse(ui.isPopupVisible(comboBox));
153: assertFalse(ui.popup.isVisible());
154: ui.popup.show();
155: assertTrue(ui.isPopupVisible(comboBox));
156: assertTrue(ui.popup.isVisible());
157: }
158:
159: public void testSetPopupVisible() throws Exception {
160: createVisibleCombo();
161: ui.popup = new BasicComboPopup(comboBox);
162: assertFalse(ui.isPopupVisible(comboBox));
163: ui.setPopupVisible(comboBox, true);
164: assertTrue(ui.isPopupVisible(comboBox));
165: ui.setPopupVisible(comboBox, false);
166: assertFalse(ui.isPopupVisible(comboBox));
167: }
168:
169: public void testIsFocusTraversable() throws Exception {
170: ui.comboBox = comboBox;
171: assertTrue(ui.isFocusTraversable(comboBox));
172: comboBox.setEditable(true);
173: assertFalse(ui.isFocusTraversable(comboBox));
174: }
175:
176: public void testGetPreferredSize() throws Exception {
177: ui.comboBox = comboBox;
178: ui.popup = new BasicComboPopup(comboBox);
179: ui.listBox = ui.popup.getList();
180: ui.installListeners();
181: assertEquals(ui.getMinimumSize(comboBox), ui
182: .getPreferredSize(comboBox));
183: comboBox.setEditable(true);
184: assertEquals(ui.getMinimumSize(comboBox), ui
185: .getPreferredSize(comboBox));
186: assertEquals(ui.getMinimumSize(null), ui.getPreferredSize(null));
187: }
188:
189: public void testGetMinimumSize() throws Exception {
190: ui.comboBox = comboBox;
191: ui.installUI(comboBox);
192: comboBox.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
193: comboBox.setFont(comboBox.getFont().deriveFont(1f));
194: Dimension listPart = new BasicComboBoxRenderer()
195: .getListCellRendererComponent(ui.listBox, "", -1,
196: false, false).getPreferredSize();
197: Dimension expectedSize = new Dimension(listPart.width
198: + listPart.height + 8, listPart.height + 8);
199: assertEquals(expectedSize, ui.getMinimumSize(null));
200: assertEquals(expectedSize, ui.cachedMinimumSize);
201: ui.cachedMinimumSize = new Dimension(100, 100);
202: assertEquals(ui.cachedMinimumSize, ui.getMinimumSize(null));
203: comboBox.addItem("aaa");
204: listPart = new BasicComboBoxRenderer()
205: .getListCellRendererComponent(ui.listBox, "aaa", -1,
206: false, false).getPreferredSize();
207: expectedSize = new Dimension(listPart.width + listPart.height
208: + 8, listPart.height + 8);
209: assertEquals(expectedSize, ui.getMinimumSize(null));
210: assertEquals(expectedSize, ui.cachedMinimumSize);
211: }
212:
213: public void testSelectNextPossibleValue() throws Exception {
214: ui.comboBox = comboBox;
215: assertEquals(-1, comboBox.getSelectedIndex());
216: ui.selectNextPossibleValue();
217: assertEquals(-1, comboBox.getSelectedIndex());
218: comboBox.addItem("a");
219: comboBox.addItem("b");
220: comboBox.addItem("c");
221: assertEquals(0, comboBox.getSelectedIndex());
222: ui.selectNextPossibleValue();
223: assertEquals(1, comboBox.getSelectedIndex());
224: ui.selectNextPossibleValue();
225: assertEquals(2, comboBox.getSelectedIndex());
226: ui.selectNextPossibleValue();
227: assertEquals(2, comboBox.getSelectedIndex());
228: }
229:
230: public void testSelectPreviousPossibleValue() throws Exception {
231: ui.comboBox = comboBox;
232: assertEquals(-1, comboBox.getSelectedIndex());
233: ui.selectPreviousPossibleValue();
234: assertEquals(-1, comboBox.getSelectedIndex());
235: comboBox.addItem("a");
236: comboBox.addItem("b");
237: comboBox.addItem("c");
238: assertEquals(0, comboBox.getSelectedIndex());
239: ui.selectPreviousPossibleValue();
240: assertEquals(0, comboBox.getSelectedIndex());
241: comboBox.setSelectedIndex(2);
242: ui.selectPreviousPossibleValue();
243: assertEquals(1, comboBox.getSelectedIndex());
244: ui.selectPreviousPossibleValue();
245: assertEquals(0, comboBox.getSelectedIndex());
246: ui.selectPreviousPossibleValue();
247: assertEquals(0, comboBox.getSelectedIndex());
248: }
249:
250: public void testToggleOpenClose() throws Exception {
251: createVisibleCombo();
252: ui.popup = new BasicComboPopup(comboBox);
253: assertFalse(ui.isPopupVisible(comboBox));
254: ui.toggleOpenClose();
255: assertTrue(ui.isPopupVisible(comboBox));
256: ui.toggleOpenClose();
257: assertFalse(ui.isPopupVisible(comboBox));
258: }
259:
260: public void testRectangleForCurrentValue() throws Exception {
261: ui.comboBox = comboBox;
262: Rectangle r1 = ui.rectangleForCurrentValue();
263: assertEquals(new Rectangle(0, 0, 0, 0), r1);
264: Rectangle r2 = ui.rectangleForCurrentValue();
265: assertFalse(r1 == r2);
266: }
267:
268: // Regression test for HARMONY-2896
269: public void testGetAccessibleChildrenCount() throws Exception {
270: ui.comboBox = null;
271: try {
272: ui.getAccessibleChildrenCount(new JComponent() {
273: });
274: fail("NullPointerException should have been thrown");
275: } catch (NullPointerException e) {
276: // Expected
277: }
278: try {
279: ui.getAccessibleChildrenCount(null);
280: fail("NullPointerException should have been thrown");
281: } catch (NullPointerException e) {
282: // Expected
283: }
284: }
285:
286: public void testGetInsets() throws Exception {
287: ui.comboBox = comboBox;
288: assertEquals(comboBox.getInsets(), ui.getInsets());
289: comboBox.setBorder(BorderFactory.createEmptyBorder(1, 2, 3, 4));
290: assertEquals(comboBox.getInsets(), ui.getInsets());
291: assertEquals(new Insets(1, 2, 3, 4), ui.getInsets());
292: }
293:
294: public void testGetDefaultSize() throws Exception {
295: ui.comboBox = comboBox;
296: ui.listBox = new BasicComboPopup(comboBox).getList();
297: if (isHarmony()) {
298: comboBox.setFont(comboBox.getFont().deriveFont(40f));
299: }
300: assertEquals(comboBox.getRenderer()
301: .getListCellRendererComponent(ui.listBox, "", 0, false,
302: false).getPreferredSize(), ui.getDefaultSize());
303: comboBox.addItem("a");
304: assertEquals(comboBox.getRenderer()
305: .getListCellRendererComponent(ui.listBox, "", 0, false,
306: false).getPreferredSize(), ui.getDefaultSize());
307: }
308:
309: public void testGetDisplaySize() throws Exception {
310: ui.comboBox = comboBox;
311: ui.popup = new BasicComboPopup(comboBox);
312: ui.listBox = ui.popup.getList();
313: ui.installListeners();
314: comboBox.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
315: assertEquals(new BasicComboBoxRenderer()
316: .getListCellRendererComponent(ui.listBox, "", -1,
317: false, false).getPreferredSize(), ui
318: .getDisplaySize());
319: comboBox.addItem("a");
320: assertEquals(new BasicComboBoxRenderer()
321: .getListCellRendererComponent(ui.listBox, "a", -1,
322: false, false).getPreferredSize(), ui
323: .getDisplaySize());
324: comboBox.addItem("aaaa");
325: comboBox.addItem("aa");
326: assertEquals(new BasicComboBoxRenderer()
327: .getListCellRendererComponent(ui.listBox, "aaaa", -1,
328: false, false).getPreferredSize(), ui
329: .getDisplaySize());
330: }
331:
332: public void testInstallDefaults() throws Exception {
333: comboBox.setFont(null);
334: comboBox.setForeground(null);
335: comboBox.setBackground(null);
336: ui.comboBox = comboBox;
337: ui.listBox = new BasicComboPopup(comboBox).getList();
338: ui.installDefaults();
339: assertNotNull(comboBox.getFont());
340: assertNotNull(comboBox.getForeground());
341: assertNotNull(comboBox.getBackground());
342: ui.uninstallDefaults();
343: }
344:
345: public void testInstallUninstallListeners() throws Exception {
346: ui.comboBox = comboBox;
347: ui.popup = new BasicComboPopup(comboBox);
348: ui.uninstallListeners();
349: int focusListenerCount = ui.comboBox.getFocusListeners().length;
350: int itemListenerCount = ui.comboBox.getItemListeners().length;
351: int keyListenerCount = ui.comboBox.getKeyListeners().length;
352: int actionListenerCount = ui.comboBox.getActionListeners().length;
353: int propertyChangeListenerCount = ui.comboBox
354: .getPropertyChangeListeners().length;
355: assertNull(ui.focusListener);
356: assertNull(ui.itemListener);
357: assertNull(ui.keyListener);
358: assertNull(ui.listDataListener);
359: assertNull(ui.propertyChangeListener);
360: assertNull(ui.popupMouseListener);
361: assertNull(ui.popupMouseMotionListener);
362: assertNull(ui.popupKeyListener);
363: ui.installListeners();
364: assertEquals(focusListenerCount + 1, ui.comboBox
365: .getFocusListeners().length);
366: assertEquals(keyListenerCount + 1, ui.comboBox
367: .getKeyListeners().length);
368: assertEquals(propertyChangeListenerCount + 1, ui.comboBox
369: .getPropertyChangeListeners().length);
370: assertEquals(itemListenerCount,
371: ui.comboBox.getItemListeners().length);
372: assertEquals(actionListenerCount, ui.comboBox
373: .getActionListeners().length);
374: assertNotNull(ui.focusListener);
375: assertNull(ui.itemListener);
376: assertNotNull(ui.keyListener);
377: assertNotNull(ui.listDataListener);
378: assertNotNull(ui.propertyChangeListener);
379: assertNotNull(ui.popupMouseListener);
380: assertNotNull(ui.popupMouseMotionListener);
381: assertNull(ui.popupKeyListener);
382: ui.uninstallListeners();
383: assertEquals(focusListenerCount, ui.comboBox
384: .getFocusListeners().length);
385: assertEquals(keyListenerCount,
386: ui.comboBox.getKeyListeners().length);
387: assertEquals(propertyChangeListenerCount, ui.comboBox
388: .getPropertyChangeListeners().length);
389: assertEquals(itemListenerCount,
390: ui.comboBox.getItemListeners().length);
391: assertEquals(actionListenerCount, ui.comboBox
392: .getActionListeners().length);
393: // assertNull(ui.focusListener);
394: // assertNull(ui.itemListener);
395: // assertNull(ui.keyListener);
396: // assertNull(ui.listDataListener);
397: }
398:
399: public void testInstallUninstallComponents() throws Exception {
400: ui.comboBox = comboBox;
401: ui.popup = new BasicComboPopup(comboBox);
402: ui.uninstallComponents();
403: assertEquals(0, ui.comboBox.getComponentCount());
404: assertNull(ui.arrowButton);
405: assertNull(ui.editor);
406: ui.installComponents();
407: assertEquals(2, ui.comboBox.getComponentCount());
408: assertTrue(ui.comboBox.getComponent(0) instanceof BasicArrowButton);
409: assertTrue(ui.comboBox.getComponent(1) instanceof CellRendererPane);
410: assertNotNull(ui.arrowButton);
411: assertNull(ui.editor);
412: ui.comboBox.add(new JLabel());
413: ui.uninstallComponents();
414: assertEquals(0, ui.comboBox.getComponentCount());
415: assertNull(ui.arrowButton);
416: assertNull(ui.editor);
417: comboBox.setEditable(true);
418: ui.installComponents();
419: assertNotNull(ui.arrowButton);
420: assertNotNull(ui.editor);
421: }
422:
423: public void testInstallUninstallUI() throws Exception {
424: ui.comboBox = comboBox;
425: ui.popup = new BasicComboPopup(comboBox);
426: ui.uninstallUI(null);
427: assertNull(comboBox.getLayout());
428: assertNotNull(ui.currentValuePane);
429: assertNull(ui.listBox);
430: int itemListenerCount = comboBox.getItemListeners().length;
431: ui.installUI(comboBox);
432: assertEquals(itemListenerCount + 1,
433: comboBox.getItemListeners().length);
434: assertNotNull(comboBox.getLayout());
435: assertTrue(ui.listBox == ui.popup.getList());
436: assertTrue(ui.listBox.getFont() == comboBox.getFont());
437: }
438:
439: public void testPaintCurrentValueBackground() throws Exception {
440: ui.comboBox = comboBox;
441: ui.paintCurrentValueBackground(createTestGraphics(),
442: new Rectangle(0, 0, 10, 10), true);
443: }
444:
445: // Regression test for HARMONY-2898
446: public void testPaintCurrentValueBackground_Null() throws Exception {
447: ui.comboBox = null;
448: try {
449: ui.paintCurrentValueBackground(createTestGraphics(),
450: new Rectangle(0, 0, 10, 10), true);
451: fail("NullPointerException should have been thrown");
452: } catch (NullPointerException e) {
453: // Expected
454: }
455: }
456:
457: public void testPaintCurrentValue() throws Exception {
458: ui.comboBox = comboBox;
459: ui.popup = new BasicComboPopup(comboBox);
460: ui.listBox = ui.popup.getList();
461: ui.paintCurrentValue(createTestGraphics(), new Rectangle(0, 0,
462: 10, 10), true);
463: }
464:
465: public void testPaint() throws Exception {
466: ui.comboBox = comboBox;
467: ui.popup = new BasicComboPopup(comboBox);
468: ui.listBox = ui.popup.getList();
469: ui.paint(createTestGraphics(), null);
470: }
471:
472: public void testConfigureUnconfigureEditor() throws Exception {
473: ui.comboBox = comboBox;
474: ui.editor = new JTextField();
475: int focusListenerCount = ui.editor.getFocusListeners().length;
476: ui.configureEditor();
477: assertEquals(focusListenerCount + 1, ui.editor
478: .getFocusListeners().length);
479: ui.unconfigureEditor();
480: assertEquals(focusListenerCount,
481: ui.editor.getFocusListeners().length);
482: }
483:
484: public void testConfigureUnconfigureArrowButton() throws Exception {
485: ui.configureArrowButton();
486: ui.unconfigureArrowButton();
487: ui.popup = new BasicComboPopup(comboBox);
488: ui.comboBox = comboBox;
489: ui.arrowButton = new JButton();
490: int mouseListenerCount = ui.arrowButton.getMouseListeners().length;
491: int mouseMotionListenerCount = ui.arrowButton
492: .getMouseMotionListeners().length;
493: ui.configureArrowButton();
494: assertEquals(mouseListenerCount + 1, ui.arrowButton
495: .getMouseListeners().length);
496: assertEquals(mouseMotionListenerCount + 1, ui.arrowButton
497: .getMouseMotionListeners().length);
498: ui.unconfigureArrowButton();
499: assertEquals(mouseListenerCount, ui.arrowButton
500: .getMouseListeners().length);
501: assertEquals(mouseMotionListenerCount, ui.arrowButton
502: .getMouseMotionListeners().length);
503: }
504:
505: public void testPropertyChangeHandler() throws Exception {
506: ui.installUI(comboBox);
507: Font newFont = comboBox.getFont().deriveFont(30f);
508: comboBox.setFont(newFont);
509: assertEquals(newFont, comboBox.getFont());
510: assertEquals(newFont, ui.listBox.getFont());
511: assertEquals(comboBox.getToolTipText(), ui.arrowButton
512: .getToolTipText());
513: assertEquals(comboBox.getRenderer(), ui.listBox
514: .getCellRenderer());
515: comboBox.setEnabled(false);
516: assertFalse(comboBox.isEnabled());
517: assertFalse(ui.arrowButton.isEnabled());
518: assertTrue(ui.listBox.isEnabled());
519: assertTrue(comboBox.getRenderer().getListCellRendererComponent(
520: ui.listBox, "", -1, false, false).isEnabled());
521: ui.paintCurrentValue(createTestGraphics(), new Rectangle(0, 0,
522: 10, 10), false);
523: assertTrue(comboBox.getRenderer().getListCellRendererComponent(
524: ui.listBox, "", -1, false, false).isEnabled());
525: String newTooltip = "combo tooltip";
526: comboBox.setToolTipText(newTooltip);
527: assertEquals(newTooltip, ui.arrowButton.getToolTipText());
528: ui.listBox.setCellRenderer(new DefaultListCellRenderer());
529: ListCellRenderer newRenderer = new DefaultListCellRenderer();
530: assertNotSame(ui.listBox.getCellRenderer(), comboBox
531: .getRenderer());
532: comboBox.setRenderer(newRenderer);
533: assertEquals(newRenderer, ui.listBox.getCellRenderer());
534: assertEquals(newRenderer, ui.popup.getList().getCellRenderer());
535: }
536:
537: public void testRemove_NullEditor() throws Exception {
538: // Regression test for Harmony-1749
539: BasicComboBoxUI cb = new BasicComboBoxUI();
540: cb.removeEditor();
541: }
542:
543: public void testComboBoxLayoutManager() throws Exception {
544: BasicComboBoxUI ui = new BasicComboBoxUI();
545: BasicComboBoxUI.ComboBoxLayoutManager b = ui.new ComboBoxLayoutManager();
546:
547: // Regression test for HARMONY-2886
548: try {
549: b.minimumLayoutSize(null);
550: fail("NullPointerException should have been thrown");
551: } catch (NullPointerException e) {
552: // Expected
553: }
554: try {
555: b.preferredLayoutSize(null);
556: fail("NullPointerException should have been thrown");
557: } catch (NullPointerException e) {
558: // Expected
559: }
560: }
561:
562: @SuppressWarnings("deprecation")
563: private void createVisibleCombo() {
564: frame.getContentPane().add(comboBox);
565: frame.show();
566: }
567: }
|