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.Dimension;
024: import java.awt.Font;
025: import java.awt.Insets;
026: import java.beans.PropertyChangeListener;
027: import java.util.Locale;
028:
029: import javax.swing.UIManager;
030: import javax.swing.UIManager.LookAndFeelInfo;
031: import javax.swing.border.Border;
032: import javax.swing.plaf.ComponentUI;
033: import javax.swing.plaf.metal.MetalButtonUI;
034: import javax.swing.plaf.metal.MetalLookAndFeel;
035:
036: public class UIManagerTest extends SwingTestCase {
037: Locale locale = Locale.US;
038:
039: @Override
040: public void setUp() throws Exception {
041: super .setUp();
042: removePropertyChangeListeners();
043: }
044:
045: public void testPut() {
046: UIManager.put("1", "1v");
047: assertEquals("1v", UIManager.get("1"));
048: assertEquals("1v", UIManager.getDefaults().get("1"));
049: assertNull(UIManager.getLookAndFeelDefaults().get("1"));
050: assertNull(UIManager.getLookAndFeel().getDefaults().get("1"));
051: }
052:
053: public void testGet() {
054: UIManager.put("1", "1v");
055: assertEquals("1v", UIManager.get("1"));
056: assertEquals("1v", UIManager.get("1", locale));
057: assertEquals("Close", UIManager.get(
058: "InternalFrame.closeButtonToolTip", Locale.US));
059: }
060:
061: public void testGetBorder() {
062: Object result = UIManager.get("Menu.border");
063: assertNotNull(result);
064: assertTrue(result instanceof Border);
065: result = UIManager.getBorder("Menu.border");
066: assertNotNull(result);
067: assertNotNull(UIManager.getBorder("Menu.border", locale));
068: }
069:
070: public void testGetInt() {
071: Object result = UIManager.get("SplitPane.dividerSize");
072: assertTrue(result instanceof Integer);
073: int num = UIManager.getInt("SplitPane.dividerSize");
074: assertTrue(num > 0);
075: assertTrue(UIManager.getInt("SplitPane.dividerSize", locale) > 0);
076: }
077:
078: public void testGetColor() {
079: Object result = UIManager.get("InternalFrame.borderColor");
080: assertNotNull(result);
081: assertTrue(result instanceof Color);
082: result = UIManager.getColor("InternalFrame.borderColor");
083: assertNotNull(result);
084: assertNotNull(UIManager.getColor("InternalFrame.borderColor",
085: locale));
086: }
087:
088: public void testGetInsets() {
089: Object result = UIManager.get("Button.margin");
090: assertNotNull(result);
091: assertTrue(result instanceof Insets);
092: result = UIManager.getInsets("Button.margin");
093: assertNotNull(result);
094: assertNotNull(UIManager.getInsets("Button.margin", locale));
095: }
096:
097: public void testGetFont() {
098: Object result = UIManager.get("TextPane.font");
099: assertNotNull(result);
100: assertTrue(result instanceof Font);
101: result = UIManager.getFont("TextPane.font");
102: assertNotNull(result);
103: assertNotNull(UIManager.getFont("TextPane.font", locale));
104: }
105:
106: public void testGetBoolean() {
107: Object result = UIManager.get("MenuItem.borderPainted");
108: assertNotNull(result);
109: assertTrue(result instanceof Boolean);
110: boolean b = UIManager.getBoolean("MenuItem.borderPainted");
111: assertTrue(b);
112: assertTrue(UIManager.getBoolean("MenuItem.borderPainted",
113: locale));
114: }
115:
116: public void testGetString() {
117: Object result = UIManager.get("TabbedPaneUI");
118: assertNotNull(result);
119: assertTrue(result instanceof String);
120: result = UIManager.getString("TabbedPaneUI");
121: assertNotNull(result);
122: assertNotNull(UIManager.getString("TabbedPaneUI", locale));
123: }
124:
125: public void testGetDimension() {
126: Object result = UIManager.get("Spinner.arrowButtonSize");
127: assertNotNull(result);
128: assertTrue(result instanceof Dimension);
129: result = UIManager.getDimension("Spinner.arrowButtonSize");
130: assertNotNull(result);
131: assertNotNull(UIManager.getDimension("Spinner.arrowButtonSize",
132: locale));
133: }
134:
135: public void testGetIcon() {
136: Object result = UIManager.get("Menu.arrowIcon");
137: assertNotNull(result);
138: assertTrue(result instanceof Icon);
139: result = UIManager.getIcon("Menu.arrowIcon");
140: assertNotNull(result);
141: assertNotNull(UIManager.getIcon("Menu.arrowIcon", locale));
142: }
143:
144: public void testGetUI() throws Exception {
145: UIManager.setLookAndFeel(new MetalLookAndFeel());
146: JComponent c = new JComponent() {
147: private static final long serialVersionUID = 1L;
148:
149: @Override
150: public String getUIClassID() {
151: return "ButtonUI";
152: }
153: };
154: ComponentUI ui = UIManager.getUI(c);
155: assertTrue(ui instanceof MetalButtonUI);
156: }
157:
158: public void testGetCrossPlatformLookAndFeelClassName() {
159: assertEquals("javax.swing.plaf.metal.MetalLookAndFeel",
160: UIManager.getCrossPlatformLookAndFeelClassName());
161: }
162:
163: public void testGetSystemLookAndFeelClassName() {
164: if (System.getProperty("os.name").indexOf("Linux") > 0) {
165: assertEquals("javax.swing.plaf.metal.MetalLookAndFeel",
166: UIManager.getSystemLookAndFeelClassName());
167: }
168: }
169:
170: public void testGetDefaults() {
171: assertNotNull(UIManager.getLookAndFeel());
172: assertNotNull(UIManager.getDefaults());
173: assertNotNull(UIManager.getLookAndFeelDefaults());
174: }
175:
176: public void testSetUnsupportedLF() throws Exception {
177: LookAndFeel laf = UIManager.getLookAndFeel();
178: LookAndFeel lf = createUnsupportedLF();
179: try {
180: UIManager.setLookAndFeel(lf);
181: fail("UnsupportedLookAndFeelException shall be thrown");
182: } catch (UnsupportedLookAndFeelException e) {
183: assertTrue(e.getMessage().indexOf(
184: "not supported on this platform") > 0);
185: }
186: UIManager.setLookAndFeel(laf);
187: }
188:
189: public void testSetLookAndFeel() throws Exception {
190: LookAndFeel laf = UIManager.getLookAndFeel();
191: propertyChangeController = new PropertyChangeController();
192: UIManager.addPropertyChangeListener(propertyChangeController);
193: UIManager.setLookAndFeel(new MetalLookAndFeel());
194: assertTrue(propertyChangeController.isChanged("lookAndFeel"));
195: assertTrue(UIManager.getLookAndFeel() instanceof MetalLookAndFeel);
196: UIManager.setLookAndFeel(laf);
197: UIManager
198: .removePropertyChangeListener(propertyChangeController);
199: }
200:
201: public void testLookAndFeelInfo() {
202: LookAndFeelInfo lfInfo = new LookAndFeelInfo("Metal",
203: "javax.swing.plaf.metal.MetalLookAndFeel");
204: assertEquals("Metal", lfInfo.getName());
205: assertEquals("javax.swing.plaf.metal.MetalLookAndFeel", lfInfo
206: .getClassName());
207: assertEquals(
208: "javax.swing.UIManager$LookAndFeelInfo[Metal javax.swing.plaf.metal.MetalLookAndFeel]",
209: lfInfo.toString());
210: }
211:
212: public void testPropertyChangeListeners() throws Exception {
213: LookAndFeel laf = UIManager.getLookAndFeel();
214: UIManager.setLookAndFeel(new MetalLookAndFeel());
215: propertyChangeController = new PropertyChangeController();
216: UIManager.addPropertyChangeListener(propertyChangeController);
217: assertEquals(1, UIManager.getPropertyChangeListeners().length);
218: assertFalse(propertyChangeController.isChanged());
219: assertEquals(0, UIManager.getDefaults()
220: .getPropertyChangeListeners().length);
221: assertFalse(propertyChangeController.isChanged());
222: UIManager.getDefaults();
223: assertFalse(propertyChangeController.isChanged());
224: UIManager.put("1", "1v");
225: assertFalse(propertyChangeController.isChanged());
226: MetalLookAndFeel metalLookAndFeel = new MetalLookAndFeel();
227: UIManager.setLookAndFeel(metalLookAndFeel);
228: assertTrue(propertyChangeController.isChanged("lookAndFeel"));
229: propertyChangeController.reset();
230: UIManager.setLookAndFeel(metalLookAndFeel);
231: assertFalse(propertyChangeController.isChanged());
232: UIManager
233: .removePropertyChangeListener(propertyChangeController);
234: assertEquals(0, UIManager.getPropertyChangeListeners().length);
235: UIManager.setLookAndFeel(laf);
236: }
237:
238: public void testSetInstalledLFs() {
239: LookAndFeelInfo[] previousValues = UIManager
240: .getInstalledLookAndFeels();
241: UIManager.setInstalledLookAndFeels(new LookAndFeelInfo[] {});
242: UIManager.installLookAndFeel("new", "newClass");
243: assertEquals(1, UIManager.getInstalledLookAndFeels().length);
244: assertEquals("new", UIManager.getInstalledLookAndFeels()[0]
245: .getName());
246: assertEquals("newClass",
247: UIManager.getInstalledLookAndFeels()[0].getClassName());
248: UIManager.setInstalledLookAndFeels(new LookAndFeelInfo[] {});
249: LookAndFeelInfo lfInfo0 = new LookAndFeelInfo("Metal",
250: "javax.swing.plaf.metal.MetalLookAndFeel");
251: LookAndFeelInfo lfInfo1 = new LookAndFeelInfo("-", "1");
252: UIManager.installLookAndFeel(lfInfo0);
253: assertEquals(1, UIManager.getInstalledLookAndFeels().length);
254: assertEquals(lfInfo0.getName(), UIManager
255: .getInstalledLookAndFeels()[0].getName());
256: assertEquals(lfInfo0.getClassName(), UIManager
257: .getInstalledLookAndFeels()[0].getClassName());
258: UIManager.setInstalledLookAndFeels(new LookAndFeelInfo[] {
259: lfInfo0, lfInfo1 });
260: assertEquals(2, UIManager.getInstalledLookAndFeels().length);
261: UIManager.setInstalledLookAndFeels(previousValues);
262: }
263:
264: public void testGetInstalledLFs() throws Exception {
265: LookAndFeelInfo[] installedLookAndFeels = UIManager
266: .getInstalledLookAndFeels();
267: if (isHarmony()) {
268: assertEquals(1, installedLookAndFeels.length);
269: } else {
270: assertEquals(3, installedLookAndFeels.length);
271: }
272: boolean foundMetalLF = false;
273: for (int i = 0; i < installedLookAndFeels.length; i++) {
274: if ("Metal".equals(installedLookAndFeels[i].getName())) {
275: foundMetalLF = true;
276: }
277: }
278: assertTrue(foundMetalLF);
279: }
280:
281: public void testAuxillaryLafs() {
282:
283: MetalLookAndFeel auxLaf1 = new MetalLookAndFeel();
284: MetalLookAndFeel auxLaf2 = new MetalLookAndFeel();
285:
286: assertEquals(UIManager.getAuxiliaryLookAndFeels().length, 0);
287: UIManager.addAuxiliaryLookAndFeel(auxLaf1);
288: assertEquals(UIManager.getAuxiliaryLookAndFeels().length, 1);
289: UIManager.addAuxiliaryLookAndFeel(auxLaf1);
290: assertEquals(UIManager.getAuxiliaryLookAndFeels().length, 1);
291: UIManager.addAuxiliaryLookAndFeel(auxLaf2);
292: assertEquals(UIManager.getAuxiliaryLookAndFeels().length, 2);
293: UIManager.addAuxiliaryLookAndFeel(createUnsupportedLF());
294: assertEquals(UIManager.getAuxiliaryLookAndFeels().length, 2);
295:
296: assertTrue(UIManager.removeAuxiliaryLookAndFeel(auxLaf1));
297: assertEquals(UIManager.getAuxiliaryLookAndFeels().length, 1);
298: assertFalse(UIManager.removeAuxiliaryLookAndFeel(auxLaf1));
299: assertFalse(UIManager
300: .removeAuxiliaryLookAndFeel(new MetalLookAndFeel()));
301: assertEquals(UIManager.getAuxiliaryLookAndFeels().length, 1);
302: assertTrue(UIManager.removeAuxiliaryLookAndFeel(auxLaf2));
303: assertEquals(UIManager.getAuxiliaryLookAndFeels().length, 0);
304:
305: }
306:
307: private LookAndFeel createUnsupportedLF() {
308: return new LookAndFeel() {
309: @Override
310: public String getDescription() {
311: return null;
312: }
313:
314: @Override
315: public String getID() {
316: return null;
317: }
318:
319: @Override
320: public String getName() {
321: return null;
322: }
323:
324: @Override
325: public boolean isNativeLookAndFeel() {
326: return false;
327: }
328:
329: @Override
330: public boolean isSupportedLookAndFeel() {
331: return false;
332: }
333: };
334: }
335:
336: private static void removePropertyChangeListeners() {
337: PropertyChangeListener[] array = UIManager
338: .getPropertyChangeListeners();
339: for (int i = 0; i < array.length; i++) {
340: UIManager.removePropertyChangeListener(array[i]);
341: }
342: }
343: }
|