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 Alexander T. Simbirtsev
019: * @version $Revision$
020: * Created on 29.04.2005
021:
022: */package javax.swing.plaf.basic;
023:
024: import java.awt.Color;
025: import java.awt.Dimension;
026: import java.awt.Font;
027: import java.awt.image.BufferedImage;
028: import javax.swing.AbstractButton;
029: import javax.swing.BorderFactory;
030: import javax.swing.Icon;
031: import javax.swing.ImageIcon;
032: import javax.swing.JButton;
033: import javax.swing.JCheckBox;
034: import javax.swing.JScrollBar;
035: import javax.swing.SwingTestCase;
036: import javax.swing.UIManager;
037: import javax.swing.border.Border;
038: import javax.swing.border.EmptyBorder;
039: import javax.swing.plaf.BorderUIResource;
040: import javax.swing.plaf.ColorUIResource;
041: import javax.swing.plaf.FontUIResource;
042:
043: public class BasicCheckBoxUITest extends SwingTestCase {
044: public class MyBasicCheckBoxUI extends BasicCheckBoxUI {
045: @Override
046: public String getPropertyPrefix() {
047: return super .getPropertyPrefix();
048: }
049:
050: @Override
051: public void installDefaults(final AbstractButton b) {
052: super .installDefaults(b);
053: }
054:
055: @Override
056: public void uninstallDefaults(final AbstractButton b) {
057: super .uninstallDefaults(b);
058: }
059:
060: @Override
061: public int getTextShiftOffset() {
062: return super .getTextShiftOffset();
063: }
064:
065: public void setIcon(final Icon newIcon) {
066: icon = newIcon;
067: }
068:
069: public void setTextIconGap(final int gap) {
070: defaultTextIconGap = gap;
071: }
072:
073: public void setTextShiftOffset(final int offset) {
074: defaultTextShiftOffset = offset;
075: }
076: };
077:
078: public MyBasicCheckBoxUI ui = null;
079:
080: /*
081: * @see TestCase#setUp()
082: */
083: @Override
084: protected void setUp() throws Exception {
085: super .setUp();
086: ui = new MyBasicCheckBoxUI();
087: }
088:
089: public void testGetPreferredSize() {
090: Font font = new FontUIResource(
091: new Font("serif", Font.PLAIN, 24));
092: UIManager.put("CheckBox.font", font);
093: Border border = new BorderUIResource(BorderFactory
094: .createEmptyBorder(11, 11, 11, 11));
095: UIManager.put("CheckBox.border", border);
096: JCheckBox button1 = new JCheckBox();
097: JCheckBox button2 = new JCheckBox("text");
098: JCheckBox button3 = new JCheckBox("text");
099: JCheckBox button6 = new JCheckBox("text");
100: button3.setBorder(new EmptyBorder(10, 10, 10, 10));
101: button6.setBorder(null);
102: int iconH = 20;
103: int iconW = 30;
104: ui.setIcon(new ImageIcon(new BufferedImage(iconW, iconH,
105: BufferedImage.TYPE_INT_ARGB)));
106: // following parameters are not being used by UI
107: ui.setTextIconGap(100);
108: ui.setTextShiftOffset(33);
109: int horInsets = button1.getInsets().left
110: + button1.getInsets().right;
111: int vertInsets = button1.getInsets().top
112: + button1.getInsets().bottom;
113: int textWidth = button1.getFontMetrics(button1.getFont())
114: .stringWidth("text");
115: int textHeight = button1.getFontMetrics(button1.getFont())
116: .getHeight();
117: assertEquals("PreferredSize", new Dimension(horInsets + iconW,
118: vertInsets + iconH), ui.getPreferredSize(button1));
119: assertEquals("PreferredSize", new Dimension(horInsets + iconW
120: + textWidth + button1.getIconTextGap(), vertInsets
121: + Math.max(iconH, textHeight)), ui
122: .getPreferredSize(button2));
123: horInsets = button3.getInsets().left
124: + button3.getInsets().right;
125: vertInsets = button3.getInsets().top
126: + button3.getInsets().bottom;
127: assertEquals("PreferredSize", new Dimension(horInsets + iconW
128: + textWidth + button1.getIconTextGap(), vertInsets
129: + Math.max(iconH, textHeight)), ui
130: .getPreferredSize(button3));
131: horInsets = button6.getInsets().left
132: + button6.getInsets().right;
133: vertInsets = button6.getInsets().top
134: + button6.getInsets().bottom;
135: assertEquals("PreferredSize", new Dimension(horInsets + iconW
136: + textWidth + button1.getIconTextGap(), vertInsets
137: + Math.max(iconH, textHeight)), ui
138: .getPreferredSize(button6));
139:
140: // regression test for HARMONY-2605
141: assertNull(new BasicCheckBoxUI()
142: .getMaximumSize(new JScrollBar()));
143: }
144:
145: public void testCreateUI() {
146: assertTrue("created UI is not null", null != BasicCheckBoxUI
147: .createUI(new JButton()));
148: assertTrue("created UI is of the proper class", BasicCheckBoxUI
149: .createUI(null) instanceof BasicCheckBoxUI);
150: assertTrue("created UI is not unique", BasicCheckBoxUI
151: .createUI(null) == BasicCheckBoxUI.createUI(null));
152: }
153:
154: public void testUninstallDefaults() {
155: JCheckBox button = new JCheckBox();
156: UIManager.put("CheckBox.foreground", new ColorUIResource(
157: Color.cyan));
158: UIManager.put("CheckBox.background", new ColorUIResource(
159: Color.blue));
160: assertNotNull("font is installed", button.getFont());
161: Font font = new FontUIResource(button.getFont()
162: .deriveFont(100f));
163: UIManager.put("CheckBox.font", font);
164: Border border = new BorderUIResource(BorderFactory
165: .createEmptyBorder(0, 0, 0, 0));
166: UIManager.put("CheckBox.border", border);
167: button.setUI(ui);
168: ui.installDefaults(button);
169: ui.uninstallDefaults(button);
170: assertEquals("background", Color.blue, button.getBackground());
171: assertEquals("foreground", Color.cyan, button.getForeground());
172: assertEquals("font", font, button.getFont());
173: assertNull("border", button.getBorder());
174: }
175:
176: public void testInstallDefaults() {
177: JCheckBox button = new JCheckBox();
178: UIManager.put("CheckBox.foreground", new ColorUIResource(
179: Color.cyan));
180: UIManager.put("CheckBox.background", new ColorUIResource(
181: Color.blue));
182: assertNotNull("font is installed", button.getFont());
183: Font font = new FontUIResource(button.getFont()
184: .deriveFont(100f));
185: UIManager.put("CheckBox.font", font);
186: Border border = new BorderUIResource(BorderFactory
187: .createEmptyBorder(0, 0, 0, 0));
188: UIManager.put("CheckBox.border", border);
189: button.setUI(ui);
190: ui.installDefaults(button);
191: assertEquals("background", Color.blue, button.getBackground());
192: assertEquals("foreground", Color.cyan, button.getForeground());
193: assertEquals("font", font, button.getFont());
194: assertEquals("border", border, button.getBorder());
195: }
196:
197: public void testGetPropertyPrefix() {
198: assertEquals("prefix ", "CheckBox.", ui.getPropertyPrefix());
199: }
200:
201: public void testGetDefaultIcon() {
202: assertNull("icon", ui.getDefaultIcon());
203: Icon icon = new ImageIcon();
204: ui.setIcon(icon);
205: assertEquals("icon", icon, ui.getDefaultIcon());
206: }
207: }
|