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 16.02.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.image.BufferedImage;
028: import javax.swing.BorderFactory;
029: import javax.swing.ImageIcon;
030: import javax.swing.InputMap;
031: import javax.swing.JButton;
032: import javax.swing.KeyStroke;
033: import javax.swing.SwingTestCase;
034: import javax.swing.UIManager;
035: import javax.swing.border.Border;
036: import javax.swing.border.EmptyBorder;
037: import javax.swing.plaf.BorderUIResource;
038: import javax.swing.plaf.ColorUIResource;
039: import javax.swing.plaf.FontUIResource;
040: import javax.swing.plaf.InputMapUIResource;
041:
042: public class BasicButtonUITest extends SwingTestCase {
043: protected BasicButtonUI ui;
044:
045: protected InputMap uiInputMap;
046:
047: Border previousBorder;
048:
049: @Override
050: protected void setUp() throws Exception {
051: super .setUp();
052: ui = new BasicButtonUI();
053: previousBorder = UIManager.getBorder("Button.border");
054: uiInputMap = new InputMapUIResource();
055: uiInputMap.put(KeyStroke.getKeyStroke("SPACE"), "pressed");
056: uiInputMap.put(KeyStroke.getKeyStroke("released SPACE"),
057: "released");
058: uiInputMap.put(KeyStroke.getKeyStroke("ENTER"), "pressed");
059: uiInputMap.put(KeyStroke.getKeyStroke("released ENTER"),
060: "released");
061: }
062:
063: @Override
064: protected void tearDown() throws Exception {
065: UIManager.put("Button.border", previousBorder);
066: super .tearDown();
067: }
068:
069: public void testGetPreferredSize() {
070: Font font = new FontUIResource(
071: new Font("serif", Font.PLAIN, 24));
072: UIManager.put("Button.font", font);
073: JButton button1 = new JButton();
074: JButton button2 = new JButton("text");
075: JButton button3 = new JButton("text");
076: JButton button4 = new JButton();
077: JButton button5 = new JButton("text");
078: JButton button6 = new JButton("text");
079: button3.setBorder(new EmptyBorder(10, 10, 10, 10));
080: button6.setBorder(null);
081: button4.setIcon(new ImageIcon(new BufferedImage(20, 20,
082: BufferedImage.TYPE_INT_ARGB)));
083: button5.setIcon(new ImageIcon(new BufferedImage(30, 30,
084: BufferedImage.TYPE_INT_ARGB)));
085: int horInsets = button1.getInsets().left
086: + button1.getInsets().right;
087: int vertInsets = button1.getInsets().top
088: + button1.getInsets().bottom;
089: int textWidth = button1.getFontMetrics(button1.getFont())
090: .stringWidth("text");
091: int textHeight = button1.getFontMetrics(button1.getFont())
092: .getHeight();
093: assertEquals("PreferredSize", new Dimension(horInsets,
094: vertInsets), ui.getPreferredSize(button1));
095: assertEquals("PreferredSize", new Dimension(horInsets
096: + textWidth, vertInsets + textHeight), ui
097: .getPreferredSize(button2));
098: assertEquals("PreferredSize", new Dimension(horInsets
099: + button4.getIcon().getIconWidth(), vertInsets
100: + button4.getIcon().getIconHeight()), ui
101: .getPreferredSize(button4));
102: int height = vertInsets
103: + Math.max(button5.getIcon().getIconHeight(),
104: textHeight);
105: int width = horInsets + textWidth
106: + button5.getIcon().getIconWidth()
107: + button5.getIconTextGap();
108: assertEquals("PreferredSize", new Dimension(width, height), ui
109: .getPreferredSize(button5));
110: horInsets = button3.getInsets().left
111: + button3.getInsets().right;
112: vertInsets = button3.getInsets().top
113: + button3.getInsets().bottom;
114: assertEquals("PreferredSize", new Dimension(horInsets
115: + textWidth, vertInsets + textHeight), ui
116: .getPreferredSize(button3));
117: horInsets = button6.getInsets().left
118: + button6.getInsets().right;
119: vertInsets = button6.getInsets().top
120: + button6.getInsets().bottom;
121: assertEquals("PreferredSize", new Dimension(horInsets
122: + textWidth, vertInsets + textHeight), ui
123: .getPreferredSize(button6));
124: }
125:
126: public void testGetMinimumSize() {
127: Font font = new FontUIResource(
128: new Font("serif", Font.PLAIN, 24));
129: UIManager.put("Button.font", font);
130: JButton button1 = new JButton();
131: JButton button2 = new JButton("text");
132: JButton button3 = new JButton("text");
133: JButton button4 = new JButton();
134: JButton button5 = new JButton("text");
135: button3.setBorder(new EmptyBorder(10, 10, 10, 10));
136: button4.setIcon(new ImageIcon(new BufferedImage(20, 20,
137: BufferedImage.TYPE_INT_ARGB)));
138: button5.setIcon(new ImageIcon(new BufferedImage(30, 30,
139: BufferedImage.TYPE_INT_ARGB)));
140: int horInsets = button1.getInsets().left
141: + button1.getInsets().right;
142: int vertInsets = button1.getInsets().top
143: + button1.getInsets().bottom;
144: int textWidth = button1.getFontMetrics(button1.getFont())
145: .stringWidth("text");
146: int textHeight = button1.getFontMetrics(button1.getFont())
147: .getHeight();
148: assertEquals("MinimumSize",
149: new Dimension(horInsets, vertInsets), ui
150: .getMinimumSize(button1));
151: assertEquals("MinimumSize", new Dimension(
152: horInsets + textWidth, vertInsets + textHeight), ui
153: .getMinimumSize(button2));
154: assertEquals("MinimumSize", new Dimension(horInsets
155: + button4.getIcon().getIconWidth(), vertInsets
156: + button4.getIcon().getIconHeight()), ui
157: .getMinimumSize(button4));
158: int height = vertInsets
159: + Math.max(button5.getIcon().getIconHeight(),
160: textHeight);
161: int width = horInsets + textWidth
162: + button5.getIcon().getIconWidth()
163: + button5.getIconTextGap();
164: assertEquals("MinimumSize", new Dimension(width, height), ui
165: .getMinimumSize(button5));
166: horInsets = button3.getInsets().left
167: + button3.getInsets().right;
168: vertInsets = button3.getInsets().top
169: + button3.getInsets().bottom;
170: assertEquals("MinimumSize", new Dimension(
171: horInsets + textWidth, vertInsets + textHeight), ui
172: .getMinimumSize(button3));
173: }
174:
175: public void testGetMaximumSize() {
176: Font font = new FontUIResource(
177: new Font("serif", Font.PLAIN, 24));
178: UIManager.put("Button.font", font);
179: JButton button1 = new JButton();
180: JButton button2 = new JButton("text");
181: JButton button3 = new JButton("text");
182: JButton button4 = new JButton();
183: JButton button5 = new JButton("text");
184: button3.setBorder(new EmptyBorder(10, 10, 10, 10));
185: button4.setIcon(new ImageIcon(new BufferedImage(20, 20,
186: BufferedImage.TYPE_INT_ARGB)));
187: button5.setIcon(new ImageIcon(new BufferedImage(30, 30,
188: BufferedImage.TYPE_INT_ARGB)));
189: int horInsets = button1.getInsets().left
190: + button1.getInsets().right;
191: int vertInsets = button1.getInsets().top
192: + button1.getInsets().bottom;
193: int textWidth = button1.getFontMetrics(button1.getFont())
194: .stringWidth("text");
195: int textHeight = button1.getFontMetrics(button1.getFont())
196: .getHeight();
197: assertEquals("MaximumSize",
198: new Dimension(horInsets, vertInsets), ui
199: .getMaximumSize(button1));
200: assertEquals("MaximumSize", new Dimension(
201: horInsets + textWidth, vertInsets + textHeight), ui
202: .getMaximumSize(button2));
203: assertEquals("MaximumSize", new Dimension(horInsets
204: + button4.getIcon().getIconWidth(), vertInsets
205: + button4.getIcon().getIconHeight()), ui
206: .getMaximumSize(button4));
207: int height = vertInsets
208: + Math.max(button5.getIcon().getIconHeight(),
209: textHeight);
210: int width = horInsets + textWidth
211: + button5.getIcon().getIconWidth()
212: + button5.getIconTextGap();
213: assertEquals("MaximumSize", new Dimension(width, height), ui
214: .getMaximumSize(button5));
215: horInsets = button3.getInsets().left
216: + button3.getInsets().right;
217: vertInsets = button3.getInsets().top
218: + button3.getInsets().bottom;
219: assertEquals("MaximumSize", new Dimension(
220: horInsets + textWidth, vertInsets + textHeight), ui
221: .getMaximumSize(button3));
222: }
223:
224: /**
225: * uninstallUI() and installUI() are being tested here
226: */
227: public void testInstallUninstallUI() {
228: UIManager.put("Button.focusInputMap", uiInputMap);
229: JButton button = new JButton();
230: button.setUI(ui);
231: assertTrue(
232: "there is one property listener",
233: button.getPropertyChangeListeners().length == 1
234: && button.getPropertyChangeListeners()[0] instanceof BasicButtonListener);
235: assertTrue("there is border", button.getBorder() != null);
236: assertTrue(
237: "there is one change listener",
238: button.getChangeListeners().length == 1
239: && button.getChangeListeners()[0] instanceof BasicButtonListener);
240: assertEquals(4, button.getRegisteredKeyStrokes().length);
241: ui.uninstallUI(button);
242: assertTrue("there are no property listeners", button
243: .getPropertyChangeListeners().length == 0);
244: assertNull("there ain't no border", button.getBorder());
245: assertTrue("there are no change listeners", button
246: .getChangeListeners().length == 0);
247: assertTrue("no RegisteredKeyStrokes installed", button
248: .getRegisteredKeyStrokes().length == 0);
249: assertTrue("opaque", button.isOpaque());
250: button.setOpaque(false);
251: ui.installUI(button);
252: assertTrue(
253: "there is one property listener",
254: button.getPropertyChangeListeners().length == 1
255: && button.getPropertyChangeListeners()[0] instanceof BasicButtonListener);
256: assertTrue("there is border", button.getBorder() != null);
257: assertTrue(
258: "there is one change listener",
259: button.getChangeListeners().length == 1
260: && button.getChangeListeners()[0] instanceof BasicButtonListener);
261: assertEquals(4, button.getRegisteredKeyStrokes().length);
262: if (isHarmony()) {
263: assertFalse(button.isOpaque());
264: }
265: }
266:
267: public void testInstallUninstallUI2() {
268: JButton button = new JButton();
269: BasicButtonUI buttonUI = new BasicButtonUI();
270: assertTrue("opaque", button.isOpaque());
271: UIManager.put("Button.background", new ColorUIResource(
272: Color.red));
273: UIManager.put("Button.foreground", new ColorUIResource(
274: Color.yellow));
275: Font font = new FontUIResource(button.getFont()
276: .deriveFont(100f));
277: UIManager.put("Button.font", font);
278: Border border2 = new BorderUIResource(BorderFactory
279: .createEmptyBorder());
280: UIManager.put("Button.border", border2);
281: button.setOpaque(false);
282: UIManager.put("Button.textIconGap", new Integer(100));
283: button.setUI(buttonUI);
284: assertEquals(Color.red, button.getBackground());
285: assertEquals(Color.yellow, button.getForeground());
286: assertEquals(border2, button.getBorder());
287: assertEquals(font, button.getFont());
288: if (isHarmony()) {
289: assertFalse(button.isOpaque());
290: }
291: Border border1 = BorderFactory.createEmptyBorder();
292: button.setBorder(border1);
293: button.setUI(buttonUI);
294: buttonUI.installUI(button);
295: assertEquals(border1, button.getBorder());
296: buttonUI.uninstallUI(button);
297: if (isHarmony()) {
298: assertNull(button.getBackground());
299: assertNull(button.getForeground());
300: }
301: assertNotNull(button.getBorder());
302: UIManager.put("Button.background", Color.red);
303: button.setUI(buttonUI);
304: buttonUI.uninstallUI(button);
305: if (!isHarmony()) {
306: assertEquals(Color.red, button.getBackground());
307: assertNotNull(button.getForeground());
308: } else {
309: assertNull(button.getBackground());
310: assertNull(button.getForeground());
311: }
312: assertEquals(border1, button.getBorder());
313: }
314:
315: public void testCreateUI() {
316: assertNotNull("created UI is not null", BasicButtonUI
317: .createUI(new JButton()));
318: assertTrue("created UI is of the proper class", BasicButtonUI
319: .createUI(null) instanceof BasicButtonUI);
320: assertSame("created UI is of unique", BasicButtonUI
321: .createUI(null), BasicButtonUI.createUI(null));
322: }
323:
324: public void testCreateButtonListener() {
325: JButton button1 = new JButton();
326: JButton button2 = new JButton();
327: BasicButtonListener res1 = ui.createButtonListener(button1);
328: BasicButtonListener res2 = ui.createButtonListener(button2);
329: BasicButtonListener res3 = ui.createButtonListener(button2);
330: assertNotNull("listener created", res1);
331: assertNotNull("listener created", res2);
332: assertNotNull("listener created", res3);
333: assertTrue("created listeners are unique", res2 != res1);
334: assertTrue("created listeners are unique", res2 != res3);
335: }
336:
337: /**
338: * uninstallListeners() and installListeners() are being tested here
339: */
340: public void testInstallUninstallListeners() {
341: JButton button = new JButton();
342: button.setUI(ui);
343: ui.uninstallListeners(button);
344: assertEquals("PropertyChangeListeners", 0, button
345: .getPropertyChangeListeners().length);
346: assertEquals("ChangeListeners", 0,
347: button.getChangeListeners().length);
348: ui.installListeners(button);
349: assertEquals("PropertyChangeListeners", 1, button
350: .getPropertyChangeListeners().length);
351: assertEquals("ChangeListeners", 1,
352: button.getChangeListeners().length);
353: assertTrue("listeners", button.getPropertyChangeListeners()[0]
354: .equals(button.getChangeListeners()[0]));
355: ui.uninstallListeners(button);
356: assertEquals("PropertyChangeListeners", 0, button
357: .getPropertyChangeListeners().length);
358: assertEquals("ChangeListeners", 0,
359: button.getChangeListeners().length);
360: }
361:
362: boolean findKeyStroke(final KeyStroke[] strokes,
363: final KeyStroke stroke) {
364: for (int i = 0; i < strokes.length; i++) {
365: if (strokes[i].equals(stroke)) {
366: return true;
367: }
368: }
369: return false;
370: }
371:
372: /**
373: * uninstallKeyboardActions() and installKeyboardActions() are being tested here
374: */
375: public void testInstallUninstallKeyboardActions() {
376: JButton button = new JButton();
377: button.setUI(ui);
378: UIManager.put("Button.focusInputMap", uiInputMap);
379: ui.uninstallKeyboardActions(button);
380: assertEquals("RegisteredKeyStrokes", 0, button
381: .getRegisteredKeyStrokes().length);
382: ui.installKeyboardActions(button);
383: assertEquals("RegisteredKeyStrokes", 4, button
384: .getRegisteredKeyStrokes().length);
385: ui.uninstallKeyboardActions(button);
386: assertEquals("RegisteredKeyStrokes", 0, button
387: .getRegisteredKeyStrokes().length);
388: }
389:
390: /**
391: * uninstallDefaults() and installDefaults() are being tested here
392: */
393: public void testInstallUninstallDefaults() {
394: JButton button = new JButton();
395: button.setUI(ui);
396: ui.uninstallDefaults(button);
397: assertNull("border", button.getBorder());
398: if (!isHarmony()) {
399: assertNotNull("font", button.getFont());
400: assertNotNull("margin", button.getFont());
401: } else {
402: assertNull("font", button.getFont());
403: assertNull("margin", button.getFont());
404: }
405: UIManager.put("Button.textIconGap", new Integer(100));
406: UIManager.put("Button.textShiftOffset", new Integer(1000));
407: button.setOpaque(false);
408: ui.installDefaults(button);
409: assertNotNull(button.getBorder());
410: if (isHarmony()) {
411: assertFalse(button.isOpaque());
412: }
413: assertEquals(100, ui.getDefaultTextIconGap(button));
414: assertTrue(100 != button.getIconTextGap());
415: assertEquals(1000, ui.defaultTextShiftOffset);
416: Border border = new EmptyBorder(1, 1, 1, 1);
417: button.setBorder(border);
418: ui.uninstallDefaults(button);
419: assertEquals("border ", border, button.getBorder());
420: }
421:
422: public void testGetDefaultTextIconGap() {
423: JButton button = new JButton("text");
424: assertEquals("DefaultTextIconGap", 0, ui
425: .getDefaultTextIconGap(button));
426: assertEquals("DefaultTextIconGap", 0, ui
427: .getDefaultTextIconGap(null));
428: }
429:
430: public void testGetPropertyPrefix() {
431: assertEquals("prefix", "Button.", ui.getPropertyPrefix());
432: }
433:
434: /**
435: * The test verifies that getTextShiftOffset methods returns
436: * Button.textShiftOffset property if the setTextShiftOffset method is
437: * called and returns 0 if the clearTextShiftOffset method is called
438: */
439: @SuppressWarnings({"boxing","nls"})
440: public void testTextShiftOffest() {
441:
442: int oldTextShiftOffset = UIManager
443: .getInt("Button.textShiftOffset");
444:
445: UIManager.put("Button.textShiftOffset", 5);
446:
447: BasicButtonUI currentUI = new BasicButtonUI();
448: currentUI.installUI(new JButton());
449:
450: assertEquals(currentUI.getTextShiftOffset(), 0);
451: currentUI.setTextShiftOffset();
452: assertEquals(currentUI.getTextShiftOffset(), 5);
453: currentUI.clearTextShiftOffset();
454: assertEquals(currentUI.getTextShiftOffset(), 0);
455:
456: UIManager.put("Button.textShiftOffset", oldTextShiftOffset);
457: }
458: }
|