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.metal;
021:
022: import java.awt.Component;
023: import java.awt.Dimension;
024: import java.awt.Graphics;
025: import java.awt.Insets;
026: import java.awt.image.BufferedImage;
027: import javax.swing.BorderFactory;
028: import javax.swing.CellRendererPane;
029: import javax.swing.JButton;
030: import javax.swing.JComboBox;
031: import javax.swing.JList;
032: import javax.swing.JTextField;
033: import javax.swing.SwingConstants;
034: import javax.swing.SwingTestCase;
035: import javax.swing.plaf.ComponentUI;
036: import javax.swing.plaf.basic.BasicComboBoxRenderer;
037: import javax.swing.plaf.basic.BasicComboPopup;
038: import javax.swing.plaf.basic.ComboPopup;
039:
040: public class MetalComboBoxUITest extends SwingTestCase {
041: private TestUI ui;
042:
043: private JComboBox comboBox;
044:
045: public MetalComboBoxUITest(final String name) {
046: super (name);
047: }
048:
049: @Override
050: protected void setUp() throws Exception {
051: ui = new TestUI();
052: comboBox = new JComboBox();
053: }
054:
055: @Override
056: protected void tearDown() throws Exception {
057: ui = null;
058: comboBox = null;
059: }
060:
061: public void testMetalComboBoxUI() throws Exception {
062: assertNotNull(ui.getCurrentValuePane());
063: assertFalse(ui.getCurrentValuePane().isVisible());
064: }
065:
066: public void testCreateUI() throws Exception {
067: ComponentUI ui1 = MetalComboBoxUI.createUI(comboBox);
068: assertTrue(ui1.getClass() == MetalComboBoxUI.class);
069: ComponentUI ui2 = MetalComboBoxUI.createUI(comboBox);
070: assertNotSame(ui1, ui2);
071: }
072:
073: public void testPaint() throws Exception {
074: ui.installUI(comboBox);
075: ui.paint(createGraphics(), null);
076: }
077:
078: public void testCreatePropertyChangeListener() throws Exception {
079: assertTrue(ui.createPropertyChangeListener().getClass() == MetalComboBoxUI.MetalPropertyChangeListener.class);
080: }
081:
082: public void testConfigureUnconfigureEditor() throws Exception {
083: ui.setComboBox(comboBox);
084: ui.setEditor(new JTextField());
085: ui.configureEditor();
086: }
087:
088: //TODO
089: public void testLayoutComboBox() throws Exception {
090: ui.setComboBox(comboBox);
091: ui.layoutComboBox(null,
092: (MetalComboBoxUI.MetalComboBoxLayoutManager) ui
093: .createLayoutManager());
094: }
095:
096: public void testGetMinimumSize() throws Exception {
097: ui.setComboBox(comboBox);
098: ComboPopup popup = new BasicComboPopup(comboBox);
099: ui.setPopup(popup);
100: ui.setListBox(popup.getList());
101: ui.installListeners();
102: comboBox.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
103: Dimension listPart = new BasicComboBoxRenderer()
104: .getListCellRendererComponent(popup.getList(), "", -1,
105: false, false).getPreferredSize();
106: Dimension expectedSize = new Dimension(listPart.width
107: + listPart.height + 8, listPart.height + 8);
108: assertEquals(expectedSize, ui.getMinimumSize(null));
109: assertEquals(expectedSize, ui.getCachedMinimumSize());
110: ui.setCachedMinimumSize(new Dimension(100, 100));
111: assertEquals(ui.getCachedMinimumSize(), ui.getMinimumSize(null));
112: comboBox.addItem("aaa");
113: listPart = new BasicComboBoxRenderer()
114: .getListCellRendererComponent(popup.getList(), "aaa",
115: -1, false, false).getPreferredSize();
116: expectedSize = new Dimension(listPart.width + listPart.height
117: + 8, listPart.height + 8);
118: assertEquals(expectedSize, ui.getMinimumSize(null));
119: }
120:
121: public void testCreateEditor() throws Exception {
122: assertTrue(ui.createEditor().getClass() == MetalComboBoxEditor.UIResource.class);
123: }
124:
125: public void testCreateArrowButton() throws Exception {
126: ui.setComboBox(comboBox);
127: assertTrue(ui.createArrowButton().getClass() == MetalComboBoxButton.class);
128: MetalComboBoxButton arrowButton = (MetalComboBoxButton) ui
129: .createArrowButton();
130: assertEquals("", arrowButton.getText());
131: assertEquals(comboBox, arrowButton.getComboBox());
132: assertTrue(arrowButton.getComboIcon().getClass() == MetalComboBoxIcon.class);
133: if (isHarmony()) {
134: assertFalse(arrowButton.isIconOnly());
135: } else {
136: assertTrue(arrowButton.isIconOnly());
137: }
138: assertEquals(SwingConstants.TRAILING, arrowButton
139: .getHorizontalTextPosition());
140: assertEquals(SwingConstants.CENTER, arrowButton
141: .getHorizontalAlignment());
142: assertEquals(new Insets(3, 4, 4, 6), arrowButton.getBorder()
143: .getBorderInsets(arrowButton));
144: }
145:
146: public void testCreateLayoutManager() throws Exception {
147: assertTrue(ui.createLayoutManager().getClass() == MetalComboBoxUI.MetalComboBoxLayoutManager.class);
148: }
149:
150: public void testCreatePopup() throws Exception {
151: ui.setComboBox(comboBox);
152: assertTrue(ui.createPopup() instanceof BasicComboPopup);
153: }
154:
155: private Graphics createGraphics() {
156: return new BufferedImage(10, 10, BufferedImage.TYPE_3BYTE_BGR)
157: .createGraphics();
158: }
159:
160: private class TestUI extends MetalComboBoxUI {
161: public void setComboBox(final JComboBox comboBox) {
162: this .comboBox = comboBox;
163: }
164:
165: public void setListBox(final JList list) {
166: this .listBox = list;
167: }
168:
169: public void setPopup(final ComboPopup popup) {
170: this .popup = popup;
171: }
172:
173: public void setEditor(final Component c) {
174: editor = c;
175: }
176:
177: public Component getEditor() {
178: return editor;
179: }
180:
181: @Override
182: public void installListeners() {
183: super .installListeners();
184: }
185:
186: public JButton getArrowButton() {
187: return arrowButton;
188: }
189:
190: public void setArrowButton(final JButton b) {
191: arrowButton = b;
192: }
193:
194: public CellRendererPane getCurrentValuePane() {
195: return currentValuePane;
196: }
197:
198: public Dimension getCachedMinimumSize() {
199: return cachedMinimumSize;
200: }
201:
202: public void setCachedMinimumSize(final Dimension d) {
203: cachedMinimumSize = d;
204: }
205: }
206: }
|