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.table;
021:
022: import java.awt.Color;
023: import java.awt.Insets;
024: import javax.swing.BasicSwingTestCase;
025: import javax.swing.JTable;
026: import javax.swing.UIManager;
027: import javax.swing.border.EmptyBorder;
028: import javax.swing.plaf.ComponentUI;
029: import javax.swing.plaf.LabelUI;
030:
031: public class DefaultTableCellRendererTest extends BasicSwingTestCase {
032: private DefaultTableCellRenderer renderer;
033:
034: public DefaultTableCellRendererTest(final String name) {
035: super (name);
036: }
037:
038: @Override
039: protected void setUp() throws Exception {
040: renderer = new DefaultTableCellRenderer();
041: }
042:
043: @Override
044: protected void tearDown() throws Exception {
045: renderer = null;
046: }
047:
048: public void testDefaultTableCellRenderer() throws Exception {
049: assertTrue(DefaultTableCellRenderer.noFocusBorder instanceof EmptyBorder);
050: assertEquals(new Insets(1, 1, 1, 1),
051: DefaultTableCellRenderer.noFocusBorder
052: .getBorderInsets(null));
053: }
054:
055: public void testSetForegroundBackground() throws Exception {
056: PropertyChangeController controller = new PropertyChangeController();
057: renderer.addPropertyChangeListener(controller);
058: renderer.setForeground(Color.RED);
059: assertEquals(Color.RED, renderer.getForeground());
060: assertFalse(controller.isChanged());
061: renderer.setBackground(Color.GREEN);
062: assertEquals(Color.GREEN, renderer.getBackground());
063: assertFalse(controller.isChanged());
064: renderer.setBackground(null);
065: renderer.setForeground(null);
066: assertNull(renderer.getForeground());
067: assertNull(renderer.getBackground());
068: JTable table = new JTable();
069: table.setBackground(Color.BLUE);
070: table.setForeground(Color.RED);
071: table.setSelectionBackground(Color.MAGENTA);
072: table.setSelectionForeground(Color.CYAN);
073: assertNull(renderer.getForeground());
074: assertNull(renderer.getBackground());
075: assertSame(renderer, renderer.getTableCellRendererComponent(
076: table, null, false, false, 0, 0));
077: assertEquals(table.getBackground(), renderer.getBackground());
078: assertEquals(table.getForeground(), renderer.getForeground());
079: renderer.setForeground(Color.GRAY);
080: renderer.setBackground(Color.YELLOW);
081: renderer.getTableCellRendererComponent(table, null, false,
082: false, 0, 0);
083: if (isHarmony()) {
084: assertEquals(table.getBackground(), renderer
085: .getBackground());
086: assertEquals(table.getForeground(), renderer
087: .getForeground());
088: } else {
089: assertEquals(Color.YELLOW, renderer.getBackground());
090: assertEquals(Color.GRAY, renderer.getForeground());
091: }
092: renderer.getTableCellRendererComponent(table, null, true,
093: false, 0, 0);
094: assertEquals(table.getSelectionBackground(), renderer
095: .getBackground());
096: assertEquals(table.getSelectionForeground(), renderer
097: .getForeground());
098: }
099:
100: public void testUpdateUI() throws Exception {
101: ComponentUI ui = renderer.getUI();
102: assertTrue(ui instanceof LabelUI);
103: renderer.updateUI();
104: assertSame(ui, renderer.getUI());
105: }
106:
107: public void testGetTableCellRendererComponent() throws Exception {
108: JTable table = new JTable();
109: table.setBackground(Color.BLUE);
110: table.setForeground(Color.RED);
111: table.setSelectionBackground(Color.MAGENTA);
112: table.setSelectionForeground(Color.CYAN);
113: assertSame(renderer, renderer.getTableCellRendererComponent(
114: table, null, false, false, 0, 0));
115: assertEquals("", renderer.getText());
116: assertSame(DefaultTableCellRenderer.noFocusBorder, renderer
117: .getBorder());
118: assertEquals(table.getBackground(), renderer.getBackground());
119: assertEquals(table.getForeground(), renderer.getForeground());
120: assertEquals(table.getFont(), renderer.getFont());
121: assertSame(renderer, renderer.getTableCellRendererComponent(
122: table, "any value", false, true, -1, -1));
123: assertEquals("any value", renderer.getText());
124: assertSame(UIManager
125: .getBorder("Table.focusCellHighlightBorder"), renderer
126: .getBorder());
127: assertEquals(UIManager.getColor("Table.focusCellBackground"),
128: renderer.getBackground());
129: assertEquals(UIManager.getColor("Table.focusCellForeground"),
130: renderer.getForeground());
131: assertEquals(table.getFont(), renderer.getFont());
132: assertSame(renderer, renderer.getTableCellRendererComponent(
133: table, "any value", true, false, 0, 0));
134: assertEquals("any value", renderer.getText());
135: assertSame(DefaultTableCellRenderer.noFocusBorder, renderer
136: .getBorder());
137: assertEquals(table.getSelectionBackground(), renderer
138: .getBackground());
139: assertEquals(table.getSelectionForeground(), renderer
140: .getForeground());
141: assertEquals(table.getFont(), renderer.getFont());
142: assertSame(renderer, renderer.getTableCellRendererComponent(
143: table, "any value", true, true, -1, -1));
144: assertEquals("any value", renderer.getText());
145: assertSame(UIManager
146: .getBorder("Table.focusCellHighlightBorder"), renderer
147: .getBorder());
148: assertEquals(table.getSelectionBackground(), renderer
149: .getBackground());
150: assertEquals(table.getSelectionForeground(), renderer
151: .getForeground());
152: assertEquals(table.getFont(), renderer.getFont());
153: table.addColumn(new TableColumn());
154: assertSame(renderer, renderer.getTableCellRendererComponent(
155: table, "any value", true, true, 0, 0));
156: assertEquals("any value", renderer.getText());
157: assertSame(UIManager
158: .getBorder("Table.focusCellHighlightBorder"), renderer
159: .getBorder());
160: assertEquals(table.getSelectionBackground(), renderer
161: .getBackground());
162: assertEquals(table.getSelectionForeground(), renderer
163: .getForeground());
164: assertEquals(table.getFont(), renderer.getFont());
165: table.setModel(new DefaultTableModel() {
166: private static final long serialVersionUID = 1L;
167:
168: @Override
169: public boolean isCellEditable(final int row,
170: final int column) {
171: return false;
172: }
173: });
174: table.addColumn(new TableColumn());
175: assertSame(renderer, renderer.getTableCellRendererComponent(
176: table, "any value", true, true, 0, 0));
177: assertEquals("any value", renderer.getText());
178: assertSame(UIManager
179: .getBorder("Table.focusCellHighlightBorder"), renderer
180: .getBorder());
181: assertEquals(table.getSelectionBackground(), renderer
182: .getBackground());
183: assertEquals(table.getSelectionForeground(), renderer
184: .getForeground());
185: assertEquals(table.getFont(), renderer.getFont());
186:
187: // regression test for HARMONY-1721
188: DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
189: Color c = renderer.getBackground();
190: assertNull(c);
191: }
192:
193: public void testIsOpaque() throws Exception {
194: assertTrue(renderer.isOpaque());
195: }
196:
197: public void testFirePropertyChange() throws Exception {
198: PropertyChangeController controller = new PropertyChangeController();
199: renderer.addPropertyChangeListener(controller);
200: renderer.firePropertyChange("any", "a", "b");
201: assertFalse(controller.isChanged());
202: renderer.firePropertyChange("any", true, false);
203: assertFalse(controller.isChanged());
204: }
205:
206: public void testSetText() throws Exception {
207: assertEquals("", renderer.getText());
208: renderer.setValue(null);
209: assertEquals("", renderer.getText());
210: renderer.setValue("any");
211: assertEquals("any", renderer.getText());
212: renderer.setValue(new Integer("4"));
213: assertEquals("4", renderer.getText());
214: }
215:
216: public void testGetName() {
217: DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
218: assertNull(cr.getName());
219: }
220: }
|