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: package javax.swing.plaf.basic;
018:
019: import java.awt.Color;
020: import java.awt.Dimension;
021: import javax.swing.JComponent;
022: import javax.swing.JLabel;
023: import javax.swing.SwingTestCase;
024: import javax.swing.UIManager;
025: import javax.swing.plaf.ColorUIResource;
026: import javax.swing.plaf.ComponentUI;
027:
028: public class BasicLabelUITest extends SwingTestCase {
029: private JLabel label;
030:
031: private BasicLabelUI ui;
032:
033: public BasicLabelUITest(final String name) {
034: super (name);
035: }
036:
037: @Override
038: protected void setUp() throws Exception {
039: label = new JLabel();
040: ui = new BasicLabelUI();
041: }
042:
043: @Override
044: protected void tearDown() throws Exception {
045: ui = null;
046: label = null;
047: }
048:
049: public void testCreateUI() throws Exception {
050: ComponentUI componentUI = BasicLabelUI.createUI(new JLabel());
051: assertTrue(componentUI instanceof BasicLabelUI);
052: assertEquals(componentUI, BasicLabelUI.createUI(new JLabel()));
053: }
054:
055: public void testGetMinimumMaximumPreferredSize() throws Exception {
056: label.setText("Any");
057: label.setFont(label.getFont().deriveFont(20f));
058: Dimension prefSize = ui.getPreferredSize(label);
059: assertTrue(prefSize.width > 0);
060: assertTrue(prefSize.height > 0);
061: final Dimension d = new Dimension(100, 100);
062: ui = new BasicLabelUI() {
063: @Override
064: public Dimension getPreferredSize(final JComponent c) {
065: return d;
066: }
067: };
068: assertSame(d, ui.getPreferredSize(label));
069: assertSame(d, ui.getMinimumSize(label));
070: assertSame(d, ui.getMaximumSize(label));
071: }
072:
073: public void testInstallUI() throws Exception {
074: Color c = new ColorUIResource(Color.black);
075: label.setForeground(new ColorUIResource(c));
076: label.setBackground(new ColorUIResource(c));
077: ui.installUI(label);
078: assertEquals(UIManager.getColor("Label.foreground"), label
079: .getForeground());
080: assertEquals(UIManager.getColor("Label.background"), label
081: .getBackground());
082: }
083:
084: public void testInstallUninstallKeyboardActions() throws Exception {
085: label.getInputMap().clear();
086: label
087: .getInputMap(
088: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
089: .clear();
090: label.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).clear();
091: assertEquals(0, label.getInputMap().size());
092: assertEquals(0, label.getInputMap(
093: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).size());
094: assertEquals(0, label.getInputMap(
095: JComponent.WHEN_IN_FOCUSED_WINDOW).size());
096: ui.installKeyboardActions(label);
097: assertEquals(0, label.getInputMap().size());
098: assertEquals(0, label.getInputMap(
099: JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).size());
100: assertEquals(0, label.getInputMap(
101: JComponent.WHEN_IN_FOCUSED_WINDOW).size());
102: }
103:
104: public void testInstallUninstallComponents() throws Exception {
105: label.removeAll();
106: assertEquals(0, label.getComponentCount());
107: ui.installComponents(label);
108: assertEquals(0, label.getComponentCount());
109: }
110:
111: private class BasicLabelUIForTest extends BasicLabelUI {
112: public BasicLabelUIForTest() {
113: super ();
114: }
115:
116: public void uninstallDefaults(JLabel l) {
117: super .uninstallDefaults(l);
118: }
119: }
120:
121: /**
122: * Regression test for HARMONY-2637
123: * */
124: public void testUninstallDefaults() throws NullPointerException {
125: BasicLabelUIForTest bu = new BasicLabelUIForTest();
126: bu.uninstallDefaults(null);
127: }
128:
129: public void testInstallUI_getInheritsPopupMenu()
130: throws NullPointerException {
131: // Regression test for HARMONY-2570
132: JLabel label = new JLabel();
133: ui.installUI(label);
134: assertTrue(label.getInheritsPopupMenu());
135: }
136: }
|