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 Sergey Burlak
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Color;
023: import java.awt.Font;
024: import java.awt.Toolkit;
025: import java.lang.reflect.Method;
026: import javax.swing.plaf.ColorUIResource;
027: import javax.swing.plaf.ComponentInputMapUIResource;
028: import javax.swing.plaf.FontUIResource;
029: import javax.swing.plaf.InputMapUIResource;
030: import javax.swing.plaf.metal.MetalLookAndFeel;
031: import javax.swing.text.DefaultEditorKit;
032: import javax.swing.text.JTextComponent;
033:
034: public class LookAndFeelTest extends SwingTestCase {
035: LookAndFeel lf;
036:
037: @Override
038: public void setUp() throws Exception {
039: super .setUp();
040: lf = new LookAndFeel() {
041: @Override
042: public String getDescription() {
043: return "description";
044: }
045:
046: @Override
047: public String getID() {
048: return "id";
049: }
050:
051: @Override
052: public String getName() {
053: return "name";
054: }
055:
056: @Override
057: public boolean isNativeLookAndFeel() {
058: return false;
059: }
060:
061: @Override
062: public boolean isSupportedLookAndFeel() {
063: return false;
064: }
065: };
066: timeoutDelay = 5 * DEFAULT_TIMEOUT_DELAY;
067: }
068:
069: @Override
070: public void tearDown() throws Exception {
071: lf = null;
072: super .tearDown();
073: }
074:
075: public void testToString() {
076: assertEquals("[" + lf.getDescription() + " - "
077: + lf.getClass().getName() + "]", lf.toString());
078: }
079:
080: public void testMakeComponentInputMap() {
081: Object[] keys = new Object[] { "SPACE", "pressed" };
082: JButton button = new JButton();
083: ComponentInputMap componentInputMap = LookAndFeel
084: .makeComponentInputMap(button, keys);
085: assertTrue(componentInputMap instanceof ComponentInputMapUIResource);
086: assertTrue(componentInputMap.getComponent() instanceof JButton);
087: assertEquals(button, componentInputMap.getComponent());
088: assertEquals(1, componentInputMap.size());
089: }
090:
091: public void testMakeIcon() {
092: Object icon = LookAndFeel.makeIcon(lf.getClass(), "empty_path");
093: assertNull(((UIDefaults.LazyValue) icon)
094: .createValue(new UIDefaults()));
095: lf = new MetalLookAndFeel();
096: icon = LookAndFeel.makeIcon(MetalLookAndFeel.class,
097: "icons/TreeLeaf.gif");
098: assertTrue(icon instanceof UIDefaults.LazyValue);
099: }
100:
101: public void testMakeInputMap() {
102: Object[] keys = new Object[] { "SPACE", "pressed" };
103: InputMap inputMap = LookAndFeel.makeInputMap(keys);
104: assertTrue(inputMap instanceof InputMapUIResource);
105: assertEquals(1, inputMap.size());
106: }
107:
108: public void testMakeKeyBindings() {
109: Object[] binds = { "UP", DefaultEditorKit.beepAction, "DOWN",
110: DefaultEditorKit.beginWordAction, "TAB",
111: DefaultEditorKit.beginWordAction };
112: JTextComponent.KeyBinding[] b = LookAndFeel
113: .makeKeyBindings(binds);
114: assertEquals(3, b.length);
115: assertEquals(DefaultEditorKit.beepAction, b[0].actionName);
116: assertEquals(DefaultEditorKit.beginWordAction, b[1].actionName);
117: }
118:
119: public void testMakeKeyBindingsFromErrorArray() {
120: Object[] binds = { "UP", DefaultEditorKit.beepAction, "DOWN" };
121: try {
122: LookAndFeel.makeKeyBindings(binds);
123: fail("shall throw ArrayIndexOutOfBoundsException");
124: } catch (ArrayIndexOutOfBoundsException e) {
125: }
126: }
127:
128: public void testGetDefaults() {
129: UIDefaults defaults = lf.getDefaults();
130: assertNull(defaults);
131: }
132:
133: public void testInstallColors() {
134: lf = new MetalLookAndFeel();
135: JLabel label = new JLabel();
136: LookAndFeel.installColors(label, "Tree.selectionBackground",
137: "TextPane.background");
138: UIDefaults defaults = lf.getDefaults();
139: assertNotNull(defaults);
140: assertEquals(UIManager.get("Tree.selectionBackground"), label
141: .getBackground());
142: assertEquals(UIManager.get("TextPane.background"), label
143: .getForeground());
144: }
145:
146: public void testInstallErrorColors() {
147: lf = new MetalLookAndFeel();
148: JLabel label = new JLabel();
149: LookAndFeel.installColors(label, "bbb", "fff");
150: assertNull(label.getForeground());
151: assertNull(label.getBackground());
152: }
153:
154: public void testInstallColorAndFonts() {
155: lf = new MetalLookAndFeel();
156: JLabel label = new JLabel();
157: LookAndFeel.installColorsAndFont(label,
158: "Tree.selectionBackground", "TextPane.background",
159: "CheckBox.font");
160: UIDefaults defaults = lf.getDefaults();
161: assertNotNull(defaults);
162: assertEquals(UIManager.get("Tree.selectionBackground"), label
163: .getBackground());
164: assertEquals(UIManager.get("TextPane.background"), label
165: .getForeground());
166: assertEquals(UIManager.get("CheckBox.font"), label.getFont());
167: }
168:
169: public void testInstallUninstallBorder()
170: throws NullPointerException {
171: lf = new MetalLookAndFeel();
172: JPanel p = new JPanel();
173: UIDefaults defaults = lf.getDefaults();
174: assertNotNull(defaults);
175: try {
176: LookAndFeel.installBorder(null, "Menu.border");
177: fail("NullPointerException shall be thrown");
178: } catch (NullPointerException e) {
179: }
180: LookAndFeel.installBorder(p, "Menu.border");
181: assertEquals(UIManager.get("Menu.border"), p.getBorder());
182: LookAndFeel.uninstallBorder(p);
183: assertNull(p.getBorder());
184:
185: BasicMenuBarUIExt m = new BasicMenuBarUIExt();
186: m.uninstallDefaults();
187: }
188:
189: class BasicMenuBarUIExt extends
190: javax.swing.plaf.basic.BasicMenuBarUI {
191: public void uninstallDefaults() {
192: super .uninstallDefaults();
193: }
194: }
195:
196: public void testLoadKeyBindings() {
197: InputMap map = new InputMap();
198: Object[] binds = { "SPACE", DefaultEditorKit.beepAction,
199: KeyStroke.getKeyStroke("DOWN"),
200: DefaultEditorKit.beginWordAction };
201: try {
202: LookAndFeel.loadKeyBindings(null, binds);
203: fail("NullPointerException shall be thrown");
204: } catch (NullPointerException e) {
205: }
206: LookAndFeel.loadKeyBindings(map, null);
207: assertEquals(0, map.size());
208: LookAndFeel.loadKeyBindings(map, binds);
209: assertEquals(2, map.size());
210: }
211:
212: public void testGetDesktopPropertyValue() throws Exception {
213: Method setProperty = Toolkit.class.getDeclaredMethod(
214: "setDesktopProperty", new Class[] { String.class,
215: Object.class });
216: setProperty.setAccessible(true);
217: final Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
218: setProperty.invoke(defaultToolkit, new Object[] {
219: "win.icon.hspacing", new Integer(1) });
220: setProperty.invoke(defaultToolkit, new Object[] {
221: "win.frame.captionFont",
222: new Font("arial", Font.BOLD, 10) });
223: setProperty.invoke(defaultToolkit, new Object[] {
224: "win.frame.activeCaptionColor", Color.red });
225: assertTrue(LookAndFeel.getDesktopPropertyValue(
226: "win.icon.hspacing", "") instanceof Integer);
227: assertTrue(LookAndFeel.getDesktopPropertyValue(
228: "win.frame.captionFont", "") instanceof FontUIResource);
229: Object desktopPropertyValue = LookAndFeel
230: .getDesktopPropertyValue(
231: "win.frame.activeCaptionColor", "default");
232: assertTrue(desktopPropertyValue instanceof ColorUIResource);
233: assertTrue(desktopPropertyValue instanceof ColorUIResource);
234: assertEquals(defaultToolkit
235: .getDesktopProperty("win.frame.activeCaptionColor"),
236: desktopPropertyValue);
237: assertEquals("default", LookAndFeel.getDesktopPropertyValue(
238: "win.frame.activeCaptionColor???", "default"));
239: }
240:
241: public void testGetSupportsWindowsDecorations() {
242: assertFalse(lf.getSupportsWindowDecorations());
243: }
244:
245: public void testInstallProperty() throws Exception {
246: if (!isHarmony()) {
247: return;
248: }
249: JComponent comp1 = new JPanel();
250: JButton comp2 = new JButton();
251: LookAndFeel.installProperty(comp1, "opaque", Boolean.TRUE);
252: assertTrue("opaque", comp1.isOpaque());
253: LookAndFeel.installProperty(comp1, "opaque", Boolean.FALSE);
254: assertFalse("opaque", comp1.isOpaque());
255: comp1.setOpaque(true);
256: LookAndFeel.installProperty(comp1, "opaque", Boolean.FALSE);
257: assertTrue("opaque", comp1.isOpaque());
258: LookAndFeel.installProperty(comp2, "opaque", Boolean.TRUE);
259: assertTrue("opaque", comp2.isOpaque());
260: LookAndFeel.installProperty(comp2, "opaque", Boolean.FALSE);
261: assertFalse("opaque", comp2.isOpaque());
262: comp1.setOpaque(true);
263: LookAndFeel.installProperty(comp2, "opaque", Boolean.FALSE);
264: assertFalse("opaque", comp2.isOpaque());
265: try {
266: LookAndFeel.installProperty(comp1, "iconTextGap",
267: Boolean.TRUE);
268: fail("IllegalArgumentException shall be thrown");
269: } catch (IllegalArgumentException e) {
270: // expected
271: }
272: try {
273: LookAndFeel.installProperty(comp2, "iconTextGap",
274: Boolean.TRUE);
275: fail("ClassCastException shall be thrown");
276: } catch (ClassCastException e) {
277: // expected
278: }
279: LookAndFeel.installProperty(comp2, "iconTextGap",
280: new Integer(0));
281: assertEquals("iconTextGap", 0, comp2.getIconTextGap());
282: LookAndFeel.installProperty(comp2, "iconTextGap", new Integer(
283: 120));
284: assertEquals("iconTextGap", 120, comp2.getIconTextGap());
285: comp2.setIconTextGap(300);
286: LookAndFeel.installProperty(comp2, "iconTextGap", new Integer(
287: 120));
288: assertEquals("iconTextGap", 300, comp2.getIconTextGap());
289: }
290: }
|