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.tree;
021:
022: import java.awt.Color;
023: import java.awt.Component;
024: import java.awt.Dimension;
025: import java.awt.Font;
026: import java.awt.Graphics;
027: import javax.swing.BasicSwingTestCase;
028: import javax.swing.Icon;
029: import javax.swing.JLabel;
030: import javax.swing.JTree;
031: import javax.swing.SwingConstants;
032: import javax.swing.UIManager;
033: import javax.swing.plaf.ColorUIResource;
034: import javax.swing.plaf.FontUIResource;
035:
036: public class DefaultTreeCellRendererTest extends BasicSwingTestCase {
037: private DefaultTreeCellRenderer renderer;
038:
039: public DefaultTreeCellRendererTest(final String name) {
040: super (name);
041: }
042:
043: @Override
044: protected void setUp() throws Exception {
045: renderer = new DefaultTreeCellRenderer();
046: }
047:
048: @Override
049: protected void tearDown() throws Exception {
050: renderer = null;
051: }
052:
053: public void testDefaultTreeCellRenderer() throws Exception {
054: assertFalse(renderer.selected);
055: assertFalse(renderer.hasFocus);
056: assertNotNull(renderer.closedIcon);
057: assertNotNull(renderer.leafIcon);
058: assertNotNull(renderer.openIcon);
059: assertSame(UIManager.getColor("Tree.selectionForeground"),
060: renderer.textSelectionColor);
061: assertSame(UIManager.getColor("Tree.foreground"),
062: renderer.textNonSelectionColor);
063: assertSame(UIManager.getColor("Tree.selectionBackground"),
064: renderer.backgroundSelectionColor);
065: assertSame(UIManager.getColor("Tree.background"),
066: renderer.backgroundNonSelectionColor);
067: assertSame(UIManager.getColor("Tree.selectionBorderColor"),
068: renderer.borderSelectionColor);
069: assertEquals(SwingConstants.LEFT, renderer
070: .getHorizontalAlignment());
071: assertEquals(SwingConstants.TRAILING, renderer
072: .getHorizontalTextPosition());
073: assertEquals(SwingConstants.CENTER, renderer
074: .getVerticalAlignment());
075: assertEquals(SwingConstants.CENTER, renderer
076: .getVerticalTextPosition());
077: }
078:
079: public void testGetDefaultOpenIcon() throws Exception {
080: assertNotNull(renderer.getDefaultOpenIcon());
081: assertSame(renderer.getDefaultOpenIcon(), renderer.openIcon);
082: assertSame(UIManager.getIcon("Tree.openIcon"),
083: renderer.openIcon);
084: }
085:
086: public void testGetDefaultClosedIcon() throws Exception {
087: assertNotNull(renderer.getDefaultClosedIcon());
088: assertSame(renderer.getDefaultClosedIcon(), renderer.closedIcon);
089: assertSame(UIManager.getIcon("Tree.closedIcon"),
090: renderer.closedIcon);
091: }
092:
093: public void testGetDefaultLeafIcon() throws Exception {
094: assertNotNull(renderer.getDefaultLeafIcon());
095: assertSame(renderer.getDefaultLeafIcon(), renderer.leafIcon);
096: assertSame(UIManager.getIcon("Tree.leafIcon"),
097: renderer.leafIcon);
098: }
099:
100: public void testGetSetOpenIcon() throws Exception {
101: assertSame(renderer.getDefaultOpenIcon(), renderer
102: .getOpenIcon());
103: Icon icon = createTestIcon();
104: renderer.setOpenIcon(icon);
105: assertSame(icon, renderer.openIcon);
106: assertSame(icon, renderer.getOpenIcon());
107: renderer.setOpenIcon(null);
108: assertNull(renderer.getOpenIcon());
109: }
110:
111: public void testGetSetClosedIcon() throws Exception {
112: assertSame(renderer.getDefaultClosedIcon(), renderer
113: .getClosedIcon());
114: Icon icon = createTestIcon();
115: renderer.setClosedIcon(icon);
116: assertSame(icon, renderer.closedIcon);
117: assertSame(icon, renderer.getClosedIcon());
118: renderer.setClosedIcon(null);
119: assertNull(renderer.getClosedIcon());
120: }
121:
122: public void testGetSetLeafIcon() throws Exception {
123: assertSame(renderer.getDefaultLeafIcon(), renderer
124: .getLeafIcon());
125: Icon icon = createTestIcon();
126: renderer.setLeafIcon(icon);
127: assertSame(icon, renderer.leafIcon);
128: assertSame(icon, renderer.getLeafIcon());
129: renderer.setLeafIcon(null);
130: assertNull(renderer.getLeafIcon());
131: }
132:
133: public void testGetSetTextSelectionColor() throws Exception {
134: assertSame(renderer.textSelectionColor, renderer
135: .getTextSelectionColor());
136: renderer.setTextSelectionColor(Color.BLUE);
137: assertSame(Color.BLUE, renderer.getTextSelectionColor());
138: assertSame(renderer.textSelectionColor, renderer
139: .getTextSelectionColor());
140: }
141:
142: public void testGetSetTextNonSelectionColor() throws Exception {
143: assertSame(renderer.textNonSelectionColor, renderer
144: .getTextNonSelectionColor());
145: renderer.setTextNonSelectionColor(Color.MAGENTA);
146: assertSame(Color.MAGENTA, renderer.getTextNonSelectionColor());
147: assertSame(renderer.textNonSelectionColor, renderer
148: .getTextNonSelectionColor());
149: }
150:
151: public void testGetSetBackgroundSelectionColor() throws Exception {
152: assertSame(renderer.backgroundSelectionColor, renderer
153: .getBackgroundSelectionColor());
154: renderer.setBackgroundSelectionColor(Color.GREEN);
155: assertSame(Color.GREEN, renderer.getBackgroundSelectionColor());
156: assertSame(renderer.backgroundSelectionColor, renderer
157: .getBackgroundSelectionColor());
158: }
159:
160: public void testGetSetBackgroundNonSelectionColor()
161: throws Exception {
162: assertSame(renderer.backgroundNonSelectionColor, renderer
163: .getBackgroundNonSelectionColor());
164: renderer.setBackgroundNonSelectionColor(Color.GRAY);
165: assertSame(Color.GRAY, renderer
166: .getBackgroundNonSelectionColor());
167: assertSame(renderer.backgroundNonSelectionColor, renderer
168: .getBackgroundNonSelectionColor());
169: }
170:
171: public void testGetSetBorderSelectionColor() throws Exception {
172: assertSame(renderer.borderSelectionColor, renderer
173: .getBorderSelectionColor());
174: renderer.setBorderSelectionColor(Color.YELLOW);
175: assertSame(Color.YELLOW, renderer.getBorderSelectionColor());
176: assertSame(renderer.borderSelectionColor, renderer
177: .getBorderSelectionColor());
178: }
179:
180: public void testGetSetFont() throws Exception {
181: assertNull(renderer.getFont());
182: renderer.setFont(null);
183: assertNull(renderer.getFont());
184: renderer.setFont(new FontUIResource("font", 10, 0));
185: assertNull(renderer.getFont());
186: Font userFont = new Font("font", 20, 1);
187: renderer.setFont(userFont);
188: assertSame(userFont, renderer.getFont());
189: }
190:
191: public void testSetBackground() throws Exception {
192: assertNull(renderer.getBackground());
193: renderer.setBackground(null);
194: assertNull(renderer.getBackground());
195: renderer.setBackground(new ColorUIResource(Color.BLUE));
196: assertNull(renderer.getBackground());
197: renderer.setBackground(Color.RED);
198: assertSame(Color.RED, renderer.getBackground());
199: }
200:
201: public void testGetTreeCellRendererComponent() throws Exception {
202: JTree tree = new JTree();
203: tree.setFont(new Font("font", 20, 1));
204: tree.setBackground(Color.MAGENTA);
205: assertSame(renderer, renderer.getTreeCellRendererComponent(
206: tree, "value", false, false, false, 0, false));
207: assertFalse(renderer.isOpaque());
208: assertNull(renderer.getBackground());
209: assertSame(renderer.textNonSelectionColor, renderer
210: .getForeground());
211: assertEquals("value", renderer.getText());
212: assertSame(tree.getFont(), renderer.getFont());
213: assertSame(renderer.getDefaultClosedIcon(), renderer.getIcon());
214: assertNull(renderer.getBorder());
215: renderer.getTreeCellRendererComponent(tree, "value", true,
216: true, false, 0, false);
217: assertFalse(renderer.isOpaque());
218: assertNull(renderer.getBackground());
219: assertSame(renderer.textSelectionColor, renderer
220: .getForeground());
221: assertEquals("value", renderer.getText());
222: assertSame(tree.getFont(), renderer.getFont());
223: assertSame(renderer.getDefaultOpenIcon(), renderer.getIcon());
224: assertNull(renderer.getBorder());
225: renderer.getTreeCellRendererComponent(tree, "value", true,
226: true, true, 0, true);
227: assertFalse(renderer.isOpaque());
228: assertNull(renderer.getBackground());
229: assertSame(renderer.textSelectionColor, renderer
230: .getForeground());
231: assertEquals("value", renderer.getText());
232: assertSame(tree.getFont(), renderer.getFont());
233: assertSame(renderer.getDefaultLeafIcon(), renderer.getIcon());
234: assertNull(renderer.getBorder());
235: renderer.setBackgroundSelectionColor(Color.RED);
236: renderer.getTreeCellRendererComponent(tree, "value", true,
237: true, true, 0, true);
238: if (isHarmony()) {
239: assertSame(Color.RED, renderer.getBackground());
240: } else {
241: assertNull(renderer.getBackground());
242: }
243: }
244:
245: public void testPaint() throws Exception {
246: renderer.paint(createTestGraphics());
247: }
248:
249: public void testGetPreferredSize() throws Exception {
250: renderer.getTreeCellRendererComponent(new JTree(), "value",
251: true, true, true, 0, false);
252: JLabel label = new JLabel("value", renderer
253: .getDefaultLeafIcon(), SwingConstants.LEFT);
254: label.setFont(renderer.getFont());
255: Dimension baseDimension = label.getPreferredSize();
256: if (isHarmony()) {
257: assertEquals(new Dimension(baseDimension.width + 2,
258: baseDimension.height), renderer.getPreferredSize());
259: } else {
260: assertEquals(new Dimension(baseDimension.width + 3,
261: baseDimension.height), renderer.getPreferredSize());
262: }
263: }
264:
265: private Icon createTestIcon() {
266: return new Icon() {
267: public void paintIcon(final Component c, final Graphics g,
268: final int x, final int y) {
269: }
270:
271: public int getIconWidth() {
272: return 10;
273: }
274:
275: public int getIconHeight() {
276: return 10;
277: }
278: };
279: }
280: }
|