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: */package javax.swing.plaf.basic;
021:
022: import java.awt.Color;
023: import java.awt.Insets;
024: import javax.swing.JButton;
025: import javax.swing.JCheckBox;
026: import javax.swing.JCheckBoxMenuItem;
027: import javax.swing.JComponent;
028: import javax.swing.JEditorPane;
029: import javax.swing.JFormattedTextField;
030: import javax.swing.JMenu;
031: import javax.swing.JMenuItem;
032: import javax.swing.JPasswordField;
033: import javax.swing.JRadioButton;
034: import javax.swing.JRadioButtonMenuItem;
035: import javax.swing.JTextArea;
036: import javax.swing.JTextField;
037: import javax.swing.JTextPane;
038: import javax.swing.JToggleButton;
039: import javax.swing.SwingTestCase;
040: import javax.swing.UIManager;
041: import javax.swing.border.AbstractBorder;
042: import javax.swing.border.BevelBorder;
043: import javax.swing.border.Border;
044: import javax.swing.border.LineBorder;
045: import javax.swing.plaf.BorderUIResource;
046: import javax.swing.plaf.ColorUIResource;
047:
048: public class BasicBordersTest extends SwingTestCase {
049: private Color shadow;
050:
051: private Color darkShadow;
052:
053: private Color highlight;
054:
055: private Color lightHighlight;
056:
057: @Override
058: public void setUp() {
059: shadow = new Color(0, 0, 0);
060: darkShadow = new Color(0, 0, 1);
061: highlight = new Color(0, 0, 2);
062: lightHighlight = new Color(0, 0, 3);
063: }
064:
065: @Override
066: public void tearDown() {
067: shadow = null;
068: darkShadow = null;
069: highlight = null;
070: lightHighlight = null;
071: }
072:
073: public void testButtonBorderClass() {
074: BasicBorders.ButtonBorder border = new BasicBorders.ButtonBorder(
075: shadow, darkShadow, highlight, lightHighlight);
076: assertEquals(shadow, border.shadow);
077: assertEquals(darkShadow, border.darkShadow);
078: assertEquals(highlight, border.highlight);
079: assertEquals(lightHighlight, border.lightHighlight);
080: checkInsets(border, new Insets(2, 3, 3, 3));
081: }
082:
083: public void testFieldBorderClass() {
084: BasicBorders.FieldBorder border = new BasicBorders.FieldBorder(
085: shadow, darkShadow, highlight, lightHighlight);
086: assertEquals(shadow, border.shadow);
087: assertEquals(darkShadow, border.darkShadow);
088: assertEquals(highlight, border.highlight);
089: assertEquals(lightHighlight, border.lightHighlight);
090: checkInsets(border, new Insets(2, 2, 2, 2));
091: }
092:
093: public void testMarginBorderClass() {
094: BasicBorders.MarginBorder border = new BasicBorders.MarginBorder();
095: checkInsets(border, new Insets(0, 0, 0, 0));
096: assertEquals(UIManager.get("CheckBox.margin"), border
097: .getBorderInsets(new JCheckBox()));
098: assertEquals(UIManager.get("Button.margin"), border
099: .getBorderInsets(new JButton()));
100: assertEquals(UIManager.get("CheckBoxMenuItem.margin"), border
101: .getBorderInsets(new JCheckBoxMenuItem()));
102: assertEquals(UIManager.get("EditorPane.margin"), border
103: .getBorderInsets(new JEditorPane()));
104: assertEquals(UIManager.get("FormattedTextField.margin"), border
105: .getBorderInsets(new JFormattedTextField()));
106: assertEquals(UIManager.get("Menu.margin"), border
107: .getBorderInsets(new JMenu()));
108: assertEquals(UIManager.get("MenuItem.margin"), border
109: .getBorderInsets(new JMenuItem()));
110: assertEquals(UIManager.get("PasswordField.margin"), border
111: .getBorderInsets(new JPasswordField()));
112: assertEquals(UIManager.get("RadioButton.margin"), border
113: .getBorderInsets(new JRadioButton()));
114: assertEquals(UIManager.get("RadioButtonMenuItem.margin"),
115: border.getBorderInsets(new JRadioButtonMenuItem()));
116: assertEquals(UIManager.get("TextArea.margin"), border
117: .getBorderInsets(new JTextArea()));
118: assertEquals(UIManager.get("TextField.margin"), border
119: .getBorderInsets(new JTextField()));
120: assertEquals(UIManager.get("TextPane.margin"), border
121: .getBorderInsets(new JTextPane()));
122: assertEquals(UIManager.get("ToggleButton.margin"), border
123: .getBorderInsets(new JToggleButton()));
124: }
125:
126: public void testMenuBarBorderClass() {
127: BasicBorders.MenuBarBorder border = new BasicBorders.MenuBarBorder(
128: highlight, shadow);
129: checkInsets(border, new Insets(0, 0, 2, 0));
130: }
131:
132: public void testRadioButtonBorderClass() {
133: BasicBorders.RadioButtonBorder border = new BasicBorders.RadioButtonBorder(
134: shadow, darkShadow, highlight, lightHighlight);
135: checkCustomColors(border);
136: checkInsets(border, new Insets(2, 2, 2, 2));
137: }
138:
139: public void testRolloverButtonBorderClass() {
140: BasicBorders.RolloverButtonBorder border = new BasicBorders.RolloverButtonBorder(
141: shadow, darkShadow, highlight, lightHighlight);
142: checkCustomColors(border);
143: checkInsets(border, new Insets(2, 3, 3, 3));
144: }
145:
146: public void testSplitPaneBorderClass() {
147: BasicBorders.SplitPaneBorder border = new BasicBorders.SplitPaneBorder(
148: highlight, shadow);
149: assertEquals(highlight, border.highlight);
150: assertEquals(shadow, border.shadow);
151: assertEquals(new Insets(1, 1, 1, 1), border
152: .getBorderInsets(newJComponent()));
153: assertTrue(border.isBorderOpaque());
154: }
155:
156: public void testToggleButtonBorderClass() {
157: BasicBorders.ToggleButtonBorder border = new BasicBorders.ToggleButtonBorder(
158: shadow, darkShadow, highlight, lightHighlight);
159: checkCustomColors(border);
160: checkInsets(border, new Insets(2, 2, 2, 2));
161: }
162:
163: public void testGetToggleButtonBorder() {
164: JComponent testButton = newJComponent();
165: assertTrue(BasicBorders.getToggleButtonBorder() instanceof BorderUIResource.CompoundBorderUIResource);
166: BorderUIResource.CompoundBorderUIResource border = (BorderUIResource.CompoundBorderUIResource) BasicBorders
167: .getToggleButtonBorder();
168: assertTrue(border.getInsideBorder() instanceof BasicBorders.MarginBorder);
169: BasicBorders.MarginBorder insideBorder = (BasicBorders.MarginBorder) border
170: .getInsideBorder();
171: assertTrue(border.getOutsideBorder() instanceof BasicBorders.ToggleButtonBorder);
172: BasicBorders.ToggleButtonBorder outsideBorder = (BasicBorders.ToggleButtonBorder) border
173: .getOutsideBorder();
174: checkButtonBorder(outsideBorder);
175: assertEquals(new Insets(0, 0, 0, 0), insideBorder
176: .getBorderInsets(testButton));
177: assertFalse(insideBorder.isBorderOpaque());
178: }
179:
180: public void testGetTextFieldBorder() {
181: JComponent testButton = newJComponent();
182: assertTrue(BasicBorders.getTextFieldBorder() instanceof BasicBorders.FieldBorder);
183: BasicBorders.FieldBorder border = (BasicBorders.FieldBorder) BasicBorders
184: .getTextFieldBorder();
185: if (isHarmony()) {
186: assertEquals("shadow", new ColorUIResource(Color.GRAY),
187: border.shadow);
188: assertEquals("darkShadow", new ColorUIResource(
189: Color.DARK_GRAY), border.darkShadow);
190: assertEquals("highlight", new ColorUIResource(Color.WHITE),
191: border.highlight);
192: assertEquals("lightHighlight", new ColorUIResource(
193: Color.WHITE), border.lightHighlight);
194: }
195: assertFalse(border.isBorderOpaque());
196: assertEquals(new Insets(2, 2, 2, 2), border
197: .getBorderInsets(testButton));
198: }
199:
200: public void testGetSplitPaneDividerBorder() {
201: JComponent c = new JComponent() {
202: private static final long serialVersionUID = 1L;
203: };
204: Border border = BasicBorders.getSplitPaneDividerBorder();
205: assertTrue(border.isBorderOpaque());
206: assertEquals(new Insets(1, 1, 1, 1), border.getBorderInsets(c));
207: }
208:
209: public void testGetSplitPaneBorder() {
210: JComponent testButton = newJComponent();
211: assertTrue(BasicBorders.getSplitPaneBorder() instanceof BasicBorders.SplitPaneBorder);
212: BasicBorders.SplitPaneBorder border = (BasicBorders.SplitPaneBorder) BasicBorders
213: .getSplitPaneBorder();
214: assertTrue(border.isBorderOpaque());
215: assertEquals(new Insets(1, 1, 1, 1), border
216: .getBorderInsets(testButton));
217: if (isHarmony()) {
218: assertEquals(new ColorUIResource(Color.WHITE),
219: border.highlight);
220: assertEquals(new ColorUIResource(Color.DARK_GRAY),
221: border.shadow);
222: }
223: }
224:
225: public void testGetRadioButtonBorder() {
226: JComponent testButton = newJComponent();
227: assertTrue(BasicBorders.getRadioButtonBorder() instanceof BorderUIResource.CompoundBorderUIResource);
228: BorderUIResource.CompoundBorderUIResource border = (BorderUIResource.CompoundBorderUIResource) BasicBorders
229: .getRadioButtonBorder();
230: assertTrue(border.getInsideBorder() instanceof BasicBorders.MarginBorder);
231: BasicBorders.MarginBorder insideBorder = (BasicBorders.MarginBorder) border
232: .getInsideBorder();
233: assertTrue(border.getOutsideBorder() instanceof BasicBorders.RadioButtonBorder);
234: BasicBorders.RadioButtonBorder outsideBorder = (BasicBorders.RadioButtonBorder) border
235: .getOutsideBorder();
236: assertEquals(new Insets(0, 0, 0, 0), insideBorder
237: .getBorderInsets(testButton));
238: assertFalse(insideBorder.isBorderOpaque());
239: checkButtonBorder(outsideBorder);
240: }
241:
242: public void testGetProgressBarBorder() {
243: JComponent testButton = newJComponent();
244: assertTrue(BasicBorders.getProgressBarBorder() instanceof BorderUIResource.LineBorderUIResource);
245: BorderUIResource.LineBorderUIResource border = (BorderUIResource.LineBorderUIResource) BasicBorders
246: .getProgressBarBorder();
247: assertFalse(border.getRoundedCorners());
248: assertTrue(border.isBorderOpaque());
249: assertEquals(new Insets(2, 2, 2, 2), border
250: .getBorderInsets(testButton));
251: assertEquals(new Color(0, 255, 0), border.getLineColor());
252: assertEquals(2, border.getThickness());
253: }
254:
255: public void testGetMenuBarBorder() {
256: JComponent testButton = newJComponent();
257: assertTrue(BasicBorders.getMenuBarBorder() instanceof BasicBorders.MenuBarBorder);
258: BasicBorders.MenuBarBorder border = (BasicBorders.MenuBarBorder) BasicBorders
259: .getMenuBarBorder();
260: assertEquals(new Insets(0, 0, 2, 0), border
261: .getBorderInsets(testButton));
262: assertFalse(border.isBorderOpaque());
263: }
264:
265: public void testGetInternalFrameBorder() {
266: JComponent testButton = newJComponent();
267: assertTrue(BasicBorders.getInternalFrameBorder() instanceof BorderUIResource.CompoundBorderUIResource);
268: BorderUIResource.CompoundBorderUIResource border = (BorderUIResource.CompoundBorderUIResource) BasicBorders
269: .getInternalFrameBorder();
270: assertTrue(border.getInsideBorder() instanceof LineBorder);
271: LineBorder insideBorder = (LineBorder) border.getInsideBorder();
272: assertTrue(border.getOutsideBorder() instanceof BevelBorder);
273: BevelBorder outsideBorder = (BevelBorder) border
274: .getOutsideBorder();
275: assertEquals(new Insets(1, 1, 1, 1), insideBorder
276: .getBorderInsets(testButton));
277: assertEquals(new Insets(2, 2, 2, 2), outsideBorder
278: .getBorderInsets(testButton));
279: assertFalse(insideBorder.getRoundedCorners());
280: assertEquals(1, insideBorder.getThickness());
281: assertTrue(insideBorder.isBorderOpaque());
282: assertEquals(0, outsideBorder.getBevelType());
283: if (isHarmony()) {
284: assertEquals(new ColorUIResource(Color.LIGHT_GRAY),
285: insideBorder.getLineColor());
286: assertEquals(new ColorUIResource(Color.GRAY), outsideBorder
287: .getShadowInnerColor());
288: assertEquals(new ColorUIResource(Color.DARK_GRAY),
289: outsideBorder.getShadowOuterColor());
290: assertEquals(new ColorUIResource(Color.WHITE),
291: outsideBorder.getHighlightOuterColor());
292: assertEquals(new ColorUIResource(Color.WHITE),
293: outsideBorder.getHighlightInnerColor());
294: }
295: }
296:
297: public void testGetButtonBorder() {
298: JComponent testButton = newJComponent();
299: assertTrue(BasicBorders.getButtonBorder() instanceof BorderUIResource.CompoundBorderUIResource);
300: BorderUIResource.CompoundBorderUIResource border = (BorderUIResource.CompoundBorderUIResource) BasicBorders
301: .getButtonBorder();
302: assertTrue(border.getInsideBorder() instanceof BasicBorders.MarginBorder);
303: BasicBorders.MarginBorder insideBorder = (BasicBorders.MarginBorder) border
304: .getInsideBorder();
305: assertTrue(border.getOutsideBorder() instanceof BasicBorders.ButtonBorder);
306: BasicBorders.ButtonBorder outsideBorder = (BasicBorders.ButtonBorder) border
307: .getOutsideBorder();
308: checkColors(outsideBorder);
309: assertEquals(new Insets(0, 0, 0, 0), insideBorder
310: .getBorderInsets(testButton));
311: assertEquals(new Insets(2, 3, 3, 3), outsideBorder
312: .getBorderInsets(testButton));
313: }
314:
315: private void checkButtonBorder(
316: final BasicBorders.ButtonBorder border) {
317: checkColors(border);
318: assertFalse(border.isBorderOpaque());
319: assertEquals(new Insets(2, 2, 2, 2), border
320: .getBorderInsets(newJComponent()));
321: }
322:
323: private void checkColors(final BasicBorders.ButtonBorder border) {
324: if (isHarmony()) {
325: assertEquals("shadow", new ColorUIResource(Color.GRAY),
326: border.shadow);
327: assertEquals("darkShadow", new ColorUIResource(
328: Color.DARK_GRAY), border.darkShadow);
329: assertEquals("highlight", new ColorUIResource(Color.WHITE),
330: border.highlight);
331: assertEquals("lightHighlight", new ColorUIResource(
332: Color.WHITE), border.lightHighlight);
333: }
334: JComponent button = newJComponent();
335: assertFalse("insets are not shared", border
336: .getBorderInsets(button) == border
337: .getBorderInsets(button));
338: }
339:
340: private void checkCustomColors(
341: final BasicBorders.ButtonBorder border) {
342: assertEquals("shadow", shadow, border.shadow);
343: assertEquals("darkShadow", darkShadow, border.darkShadow);
344: assertEquals("highlight", highlight, border.highlight);
345: assertEquals("lightHighlight", lightHighlight,
346: border.lightHighlight);
347: JComponent button = newJComponent();
348: assertFalse("insets are not shared", border
349: .getBorderInsets(button) == border
350: .getBorderInsets(button));
351: }
352:
353: private void checkInsets(final AbstractBorder border,
354: final Insets testInsets) {
355: assertEquals(testInsets, border
356: .getBorderInsets(newJComponent()));
357: assertFalse(border.isBorderOpaque());
358: Insets insets = new Insets(0, 0, 0, 0);
359: border.getBorderInsets(newJComponent(), insets);
360: assertEquals(border.getBorderInsets(newJComponent()), insets);
361: JComponent button = newJComponent();
362: assertFalse(border.getBorderInsets(button) == border
363: .getBorderInsets(button));
364: }
365:
366: private JComponent newJComponent() {
367: return new JComponent() {
368: private static final long serialVersionUID = 1L;
369: };
370: }
371: }
|