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