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.Color;
023: import java.awt.Dimension;
024: import java.awt.Font;
025: import javax.swing.BorderFactory;
026: import javax.swing.JLabel;
027: import javax.swing.JList;
028: import javax.swing.SwingConstants;
029: import javax.swing.SwingTestCase;
030:
031: public class BasicComboBoxRendererTest extends SwingTestCase {
032: private BasicComboBoxRenderer renderer;
033:
034: public BasicComboBoxRendererTest(final String name) {
035: super (name);
036: }
037:
038: @Override
039: protected void setUp() throws Exception {
040: renderer = new BasicComboBoxRenderer();
041: }
042:
043: @Override
044: protected void tearDown() throws Exception {
045: renderer = null;
046: }
047:
048: public void testBasicComboBoxRenderer() throws Exception {
049: assertNotNull(BasicComboBoxRenderer.noFocusBorder);
050: assertEquals(BasicComboBoxRenderer.noFocusBorder, renderer
051: .getBorder());
052: BasicComboBoxRenderer.noFocusBorder = BorderFactory
053: .createEmptyBorder(1, 1, 1, 1);
054: assertNotSame(BasicComboBoxRenderer.noFocusBorder, renderer
055: .getBorder());
056: assertEquals(BasicComboBoxRenderer.noFocusBorder,
057: new BasicComboBoxRenderer().getBorder());
058: assertEquals("", renderer.getText());
059: // Regression test for HARMONY-2646
060: assertEquals(SwingConstants.LEADING,
061: new BasicComboBoxRenderer().getHorizontalAlignment());
062:
063: }
064:
065: public void testGetListCellRendererComponent() throws Exception {
066: JList list = new JList();
067: assertEquals(renderer, renderer.getListCellRendererComponent(
068: list, "aaa", 0, false, false));
069: assertTrue(renderer.isOpaque());
070: Font newFont = list.getFont().deriveFont(20f);
071: list.setFont(newFont);
072: assertNotSame(newFont, renderer.getFont());
073: renderer.getListCellRendererComponent(list, "aaa", 0, false,
074: false);
075: assertEquals(newFont, renderer.getFont());
076: list.setForeground(Color.red);
077: list.setBackground(Color.blue);
078: list.setSelectionForeground(Color.yellow);
079: list.setSelectionBackground(Color.pink);
080: assertNotSame(Color.red, renderer.getForeground());
081: assertNotSame(Color.blue, renderer.getBackground());
082: renderer.getListCellRendererComponent(list, "aaa", 0, false,
083: false);
084: assertEquals(Color.red, renderer.getForeground());
085: assertEquals(Color.blue, renderer.getBackground());
086: renderer.getListCellRendererComponent(list, "aaa", 0, true,
087: false);
088: assertEquals(Color.yellow, renderer.getForeground());
089: assertEquals(Color.pink, renderer.getBackground());
090: list.setEnabled(false);
091: renderer.getListCellRendererComponent(list, "aaa", 0, false,
092: false);
093: assertTrue(renderer.isEnabled());
094: assertEquals(Color.red, renderer.getForeground());
095: assertEquals(Color.blue, renderer.getBackground());
096: }
097:
098: public void testGetPreferredSize() throws Exception {
099: JLabel l = new JLabel(" ");
100: l.setBorder(BasicComboBoxRenderer.noFocusBorder);
101: assertEquals(l.getPreferredSize(), renderer.getPreferredSize());
102: renderer.getListCellRendererComponent(new JList(), "aaa", 0,
103: false, false);
104: l.setText("aaa");
105: assertEquals(l.getPreferredSize(), renderer.getPreferredSize());
106: assertEquals(l.getPreferredSize(), renderer.getPreferredSize());
107: renderer.setText(null);
108: l.setText(" ");
109: assertEquals(l.getPreferredSize(), renderer.getPreferredSize());
110: renderer.setText("");
111: assertEquals(l.getPreferredSize(), renderer.getPreferredSize());
112: renderer.setPreferredSize(new Dimension(100, 100));
113: assertEquals(new Dimension(100, 100), renderer
114: .getPreferredSize());
115: }
116: }
|