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: package javax.swing;
018:
019: import java.awt.Color;
020: import java.awt.Component;
021: import java.awt.Font;
022: import java.beans.PropertyChangeEvent;
023: import java.beans.PropertyChangeListener;
024: import javax.swing.border.Border;
025:
026: public class DefaultListCellRendererTest extends SwingTestCase {
027: private DefaultListCellRenderer renderer;
028:
029: public DefaultListCellRendererTest(final String name) {
030: super (name);
031: }
032:
033: @Override
034: protected void setUp() throws Exception {
035: super .setUp();
036: renderer = new DefaultListCellRenderer();
037: }
038:
039: @Override
040: protected void tearDown() throws Exception {
041: renderer = null;
042: super .tearDown();
043: }
044:
045: public void testGetListCellRendererComponent() throws Exception {
046: JList list = new JList();
047: list.setForeground(Color.red);
048: list.setBackground(Color.blue);
049: list.setSelectionForeground(Color.pink);
050: list.setSelectionBackground(Color.yellow);
051: Component result = renderer.getListCellRendererComponent(list,
052: "one", 0, false, false);
053: assertTrue(result == renderer);
054: JLabel label = (JLabel) result;
055: assertEquals("one", label.getText());
056: assertEquals(Color.red, label.getForeground());
057: assertEquals(Color.blue, label.getBackground());
058: assertEquals(DefaultListCellRenderer.noFocusBorder, label
059: .getBorder());
060: result = renderer.getListCellRendererComponent(list, "two", 0,
061: true, false);
062: assertTrue(result == renderer);
063: label = (JLabel) result;
064: assertEquals("two", label.getText());
065: assertEquals(Color.pink, label.getForeground());
066: assertEquals(Color.yellow, label.getBackground());
067: assertEquals(DefaultListCellRenderer.noFocusBorder, label
068: .getBorder());
069: Border border = BorderFactory.createEtchedBorder();
070: DefaultListCellRenderer.noFocusBorder = border;
071: result = renderer.getListCellRendererComponent(list, "one", 0,
072: false, false);
073: assertTrue(result == renderer);
074: label = (JLabel) result;
075: assertEquals("one", label.getText());
076: assertEquals(Color.red, label.getForeground());
077: assertEquals(Color.blue, label.getBackground());
078: assertEquals(border, label.getBorder());
079: border = BorderFactory.createRaisedBevelBorder();
080: UIManager.put("List.focusCellHighlightBorder", border);
081: result = renderer.getListCellRendererComponent(list, "three",
082: 0, true, true);
083: assertTrue(result == renderer);
084: label = (JLabel) result;
085: assertEquals("three", label.getText());
086: assertEquals(Color.pink, label.getForeground());
087: assertEquals(Color.yellow, label.getBackground());
088: assertEquals(border, label.getBorder());
089: }
090:
091: public void testGetListCellRendererComponentChangesAreReflected()
092: throws Exception {
093: JList list = new JList();
094: list.setForeground(Color.red);
095: list.setBackground(Color.blue);
096: list.setSelectionForeground(Color.pink);
097: list.setSelectionBackground(Color.yellow);
098: Component result = renderer.getListCellRendererComponent(list,
099: "one", 0, false, false);
100: assertTrue(result == renderer);
101: JLabel label = (JLabel) result;
102: assertEquals("one", label.getText());
103: assertEquals(Color.red, label.getForeground());
104: assertEquals(Color.blue, label.getBackground());
105: assertEquals(list.getFont(), label.getFont());
106: assertTrue(label.isEnabled());
107: result = renderer.getListCellRendererComponent(list, "two", 0,
108: true, false);
109: assertTrue(result == renderer);
110: label = (JLabel) result;
111: assertEquals("two", label.getText());
112: assertEquals(Color.pink, label.getForeground());
113: assertEquals(Color.yellow, label.getBackground());
114: list.setForeground(Color.black);
115: list.setBackground(Color.green);
116: list.setSelectionForeground(Color.gray);
117: list.setSelectionBackground(Color.yellow);
118: Font font = list.getFont().deriveFont(100);
119: list.setFont(font);
120: list.setEnabled(false);
121: result = renderer.getListCellRendererComponent(list, "one", 0,
122: false, false);
123: assertTrue(result == renderer);
124: label = (JLabel) result;
125: assertEquals("one", label.getText());
126: assertEquals(Color.black, label.getForeground());
127: assertEquals(Color.green, label.getBackground());
128: assertEquals(font, label.getFont());
129: assertFalse(label.isEnabled());
130: result = renderer.getListCellRendererComponent(list, "two", 0,
131: true, false);
132: assertTrue(result == renderer);
133: label = (JLabel) result;
134: assertEquals("two", label.getText());
135: assertEquals(Color.gray, label.getForeground());
136: assertEquals(Color.yellow, label.getBackground());
137: }
138:
139: public void testFirePropertyChange() throws Exception {
140: TestListener listener = new TestListener();
141: renderer.addPropertyChangeListener(listener);
142: renderer.firePropertyChange("qqq", true, false);
143: renderer.firePropertyChange("qqq", (byte) 5, (byte) 7);
144: renderer.firePropertyChange("qqq", 'a', 'b');
145: renderer.firePropertyChange("qqq", 5d, 7d);
146: renderer.firePropertyChange("qqq", 5f, 7f);
147: renderer.firePropertyChange("qqq", 5, 7);
148: renderer.firePropertyChange("qqq", (long) 5, (long) 7);
149: renderer.firePropertyChange("qqq", (short) 5, (short) 7);
150: renderer.firePropertyChange("qqq", "a", "b");
151: assertNull(listener.getEvent());
152: }
153:
154: public void testIsOpaque() throws Exception {
155: // Regression test for HARMONY-2572
156: assertTrue(new DefaultListCellRenderer().isOpaque());
157: assertTrue(new DefaultListCellRenderer.UIResource().isOpaque());
158: }
159:
160: public void testGetInheritsPopupMenu() throws Exception {
161: // Regression test for HARMONY-2570
162: assertTrue(new DefaultListCellRenderer().getInheritsPopupMenu());
163: assertTrue(new DefaultListCellRenderer.UIResource()
164: .getInheritsPopupMenu());
165: }
166:
167: private class TestListener implements PropertyChangeListener {
168: private PropertyChangeEvent event;
169:
170: public void propertyChange(final PropertyChangeEvent event) {
171: this .event = event;
172: }
173:
174: public PropertyChangeEvent getEvent() {
175: return event;
176: }
177: }
178: }
|