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.Dimension;
024: import java.awt.Font;
025: import java.awt.FontMetrics;
026: import java.awt.Insets;
027: import java.awt.Toolkit;
028: import java.awt.image.BufferedImage;
029: import javax.swing.BorderFactory;
030: import javax.swing.Icon;
031: import javax.swing.ImageIcon;
032: import javax.swing.JFrame;
033: import javax.swing.JMenu;
034: import javax.swing.JMenuBar;
035: import javax.swing.JMenuItem;
036: import javax.swing.JOptionPane;
037: import javax.swing.KeyStroke;
038: import javax.swing.MenuElement;
039: import javax.swing.MenuSelectionManager;
040: import javax.swing.SwingTestCase;
041: import javax.swing.SwingUtilities;
042: import javax.swing.UIManager;
043: import javax.swing.border.Border;
044: import javax.swing.plaf.BorderUIResource;
045: import javax.swing.plaf.ColorUIResource;
046: import javax.swing.plaf.ComponentUI;
047: import javax.swing.plaf.FontUIResource;
048: import javax.swing.plaf.InsetsUIResource;
049:
050: public class BasicMenuItemUITest extends SwingTestCase {
051: protected String prefix;
052:
053: protected BasicMenuItemUI menuItemUI;
054:
055: private Icon oldArrow;
056:
057: private Icon oldCheck;
058:
059: @Override
060: protected void setUp() throws Exception {
061: super .setUp();
062: menuItemUI = new BasicMenuItemUI();
063: prefix = "MenuItem.";
064: oldArrow = UIManager.getIcon(prefix + "arrowIcon");
065: oldCheck = UIManager.getIcon(prefix + "checkIcon");
066: MenuSelectionManager.defaultManager().clearSelectedPath();
067: }
068:
069: @Override
070: protected void tearDown() throws Exception {
071: menuItemUI = null;
072: UIManager.put(prefix + "arrowIcon", oldArrow);
073: UIManager.put(prefix + "checkIcon", oldCheck);
074: super .tearDown();
075: }
076:
077: /*
078: * Test method for 'javax.swing.plaf.basic.BasicMenuItemUI.paint(Graphics, JComponent)'
079: */
080: @SuppressWarnings("deprecation")
081: public void testPaint() {
082: JFrame frame = new JFrame();
083: JMenuItem item = new JMenuItem();
084: item.setUI(menuItemUI);
085: frame.getContentPane().add(item);
086: frame.pack();
087: frame.show();
088: menuItemUI.paint(item.getGraphics(), item);
089: }
090:
091: /*
092: * Test method for 'javax.swing.plaf.basic.BasicMenuItemUI.getPreferredSize(JComponent)'
093: */
094: public void testGetSizes() {
095: JMenuItem item = new JMenuItem() {
096: private static final long serialVersionUID = 1L;
097:
098: @Override
099: public FontMetrics getFontMetrics(Font font) {
100: return BasicMenuItemUITest.this .getFontMetrics(font);
101: }
102: };
103: Border border = new BorderUIResource(BorderFactory
104: .createEmptyBorder(1, 1, 1, 1));
105: UIManager.put(prefix + "border", border);
106: Insets margin = new InsetsUIResource(1, 2, 3, 4);
107: UIManager.put("OptionPane.margin", margin);
108: menuItemUI = new BasicMenuItemUI();
109: item.setUI(menuItemUI);
110: assertNull(menuItemUI.getMinimumSize(item));
111: assertNull(menuItemUI.getMaximumSize(item));
112: assertEquals(menuItemUI.getPreferredMenuItemSize(item,
113: menuItemUI.checkIcon, menuItemUI.arrowIcon,
114: menuItemUI.defaultTextIconGap), menuItemUI
115: .getPreferredSize(item));
116: item.setIcon(new ImageIcon(new BufferedImage(10, 20,
117: BufferedImage.TYPE_INT_RGB)));
118: assertNull(menuItemUI.getMinimumSize(item));
119: assertNull(menuItemUI.getMaximumSize(item));
120: assertEquals(menuItemUI.getPreferredMenuItemSize(item,
121: menuItemUI.checkIcon, menuItemUI.arrowIcon,
122: menuItemUI.defaultTextIconGap), menuItemUI
123: .getPreferredSize(item));
124: item.setAccelerator(KeyStroke.getKeyStroke('a'));
125: assertEquals(menuItemUI.getPreferredMenuItemSize(item,
126: menuItemUI.checkIcon, menuItemUI.arrowIcon,
127: menuItemUI.defaultTextIconGap), menuItemUI
128: .getPreferredSize(item));
129: try { //Regression test for HARMONY-2695
130: menuItemUI.getMinimumSize(null);
131: fail("NullPointerException should have been thrown");
132: } catch (NullPointerException e) {
133: // Expected
134: }
135: try { //Regression test for HARMONY-2695
136: menuItemUI.getMaximumSize(null);
137: fail("NullPointerException should have been thrown");
138: } catch (NullPointerException e) {
139: // Expected
140: }
141: }
142:
143: /*
144: * Test method for 'javax.swing.plaf.basic.BasicMenuItemUI.uninstallUI(JComponent)'
145: */
146: public void testInstallUninstallUI() {
147: JMenuItem item = new JMenuItem();
148: menuItemUI.installUI(item);
149: assertNotNull(item.getBorder());
150: assertNotNull(SwingUtilities.getUIActionMap(item));
151: assertNotNull(menuItemUI.mouseInputListener);
152: assertEquals(0, item.getComponentCount());
153: menuItemUI.uninstallUI(item);
154: assertNull(item.getBorder());
155: assertNull(SwingUtilities.getUIActionMap(item));
156: assertNull(menuItemUI.mouseInputListener);
157: assertEquals(0, item.getComponentCount());
158:
159: try { //Regression test for HARMONY-2704
160: menuItemUI.uninstallUI(new JOptionPane());
161: fail("ClassCastException should have been thrown");
162: } catch (ClassCastException e) {
163: // Expected
164: }
165: }
166:
167: /*
168: * Test method for 'javax.swing.plaf.basic.BasicMenuItemUI.createUI(JComponent)'
169: */
170: public void testCreateUI() {
171: ComponentUI ui1 = BasicMenuItemUI.createUI(null);
172: ComponentUI ui2 = BasicMenuItemUI.createUI(null);
173: assertTrue(ui1 instanceof BasicMenuItemUI);
174: assertNotSame(ui1, ui2);
175: }
176:
177: /*
178: * Test method for 'javax.swing.plaf.basic.BasicMenuItemUI.installDefaults()'
179: */
180: public void testInstallUninstallDefaults() {
181: JMenuItem item = new JMenuItem();
182: item.setUI(menuItemUI);
183: UIManager.getDefaults().put(prefix + "background",
184: new ColorUIResource(Color.red));
185: UIManager.getDefaults().put(prefix + "foreground",
186: new ColorUIResource(Color.yellow));
187: UIManager.getDefaults().put(prefix + "acceleratorForeground",
188: new ColorUIResource(Color.cyan));
189: UIManager.getDefaults().put(
190: prefix + "acceleratorSelectionForeground",
191: new ColorUIResource(Color.magenta));
192: UIManager.getDefaults().put(prefix + "selectionBackground",
193: new ColorUIResource(Color.green));
194: UIManager.getDefaults().put(prefix + "selectionForeground",
195: new ColorUIResource(Color.pink));
196: UIManager.getDefaults().put(prefix + "disabledForeground",
197: new ColorUIResource(Color.orange));
198: Font font = new FontUIResource(item.getFont().deriveFont(100f));
199: UIManager.getDefaults().put(prefix + "font", font);
200: Border border = new BorderUIResource(BorderFactory
201: .createEmptyBorder(0, 0, 0, 0));
202: UIManager.getDefaults().put(prefix + "border", border);
203: Insets margin = new InsetsUIResource(1, 2, 3, 4);
204: UIManager.getDefaults().put(prefix + "margin", margin);
205: Icon arrow = new ImageIcon(new BufferedImage(10, 10,
206: BufferedImage.TYPE_INT_RGB));
207: UIManager.getDefaults().put(prefix + "arrowIcon", arrow);
208: Icon check = new ImageIcon(new BufferedImage(10, 10,
209: BufferedImage.TYPE_INT_RGB));
210: UIManager.getDefaults().put(prefix + "checkIcon", check);
211: UIManager.getDefaults().put(prefix + "borderPainted",
212: Boolean.FALSE);
213: menuItemUI.defaultTextIconGap = 1000;
214: menuItemUI.oldBorderPainted = true;
215: menuItemUI.installDefaults();
216: assertEquals(Color.red, item.getBackground());
217: assertEquals(Color.yellow, item.getForeground());
218: assertEquals(font, item.getFont());
219: assertEquals(border, item.getBorder());
220: assertEquals(margin, item.getMargin());
221: assertEquals(4, menuItemUI.defaultTextIconGap);
222: if (isHarmony()) {
223: assertFalse(menuItemUI.oldBorderPainted);
224: }
225: assertEquals(Color.cyan, menuItemUI.acceleratorForeground);
226: assertEquals(Color.magenta,
227: menuItemUI.acceleratorSelectionForeground);
228: assertEquals(Color.green, menuItemUI.selectionBackground);
229: assertEquals(Color.pink, menuItemUI.selectionForeground);
230: assertEquals(Color.orange, menuItemUI.disabledForeground);
231: assertEquals(arrow, menuItemUI.arrowIcon);
232: assertEquals(check, menuItemUI.checkIcon);
233: menuItemUI.uninstallDefaults();
234: assertNull(item.getBackground());
235: assertNull(item.getForeground());
236: assertNull(item.getFont());
237: assertNull(item.getBorder());
238: // Assertion below has been deleted because now we are compartible with
239: // RI in this point. For details see HARMONY-4655
240: // assertNull(item.getMargin());
241: assertEquals(4, menuItemUI.defaultTextIconGap);
242: if (isHarmony()) {
243: assertFalse(menuItemUI.oldBorderPainted);
244: }
245: assertEquals(Color.cyan, menuItemUI.acceleratorForeground);
246: assertEquals(Color.magenta,
247: menuItemUI.acceleratorSelectionForeground);
248: assertEquals(Color.green, menuItemUI.selectionBackground);
249: assertEquals(Color.pink, menuItemUI.selectionForeground);
250: assertEquals(Color.orange, menuItemUI.disabledForeground);
251: assertEquals(arrow, menuItemUI.arrowIcon);
252: assertEquals(check, menuItemUI.checkIcon);
253: }
254:
255: /*
256: * Test method for 'javax.swing.plaf.basic.BasicMenuItemUI.getPropertyPrefix()'
257: */
258: public void testGetPropertyPrefix() {
259: assertEquals("MenuItem", menuItemUI.getPropertyPrefix());
260: }
261:
262: /*
263: * Test method for 'javax.swing.plaf.basic.BasicMenuItemUI.installListeners()'
264: */
265: public void testInstallUninstallListeners() {
266: JMenuItem item = new JMenuItem();
267: menuItemUI = (BasicMenuItemUI) item.getUI();
268: menuItemUI.uninstallListeners();
269: assertEquals(0, item.getMenuDragMouseListeners().length);
270: assertEquals(0, item.getMenuKeyListeners().length);
271: assertEquals(0, item.getPropertyChangeListeners().length);
272: assertEquals(0, item.getMouseMotionListeners().length);
273: assertEquals(0, item.getMouseListeners().length);
274: assertNull(menuItemUI.mouseInputListener);
275: assertNull(menuItemUI.menuKeyListener);
276: assertNull(menuItemUI.menuDragMouseListener);
277: menuItemUI.menuItem = item;
278: menuItemUI.installListeners();
279: Object listener = menuItemUI.mouseInputListener;
280: assertNotNull(menuItemUI.mouseInputListener);
281: if (isHarmony()) {
282: assertNull(menuItemUI.menuKeyListener);
283: } else {
284: assertNotNull(menuItemUI.menuKeyListener);
285: }
286: assertNotNull(menuItemUI.menuDragMouseListener);
287: assertSame(menuItemUI.menuDragMouseListener, item
288: .getMenuDragMouseListeners()[0]);
289: if (isHarmony()) {
290: assertSame(listener, item.getMouseMotionListeners()[0]);
291: assertSame(listener, item.getMouseListeners()[0]);
292: }
293: assertNotNull(item.getFocusListeners()[0]);
294: }
295:
296: /*
297: * Test method for 'javax.swing.plaf.basic.BasicMenuItemUI.installKeyboardActions()'
298: */
299: public void testInstallUninstallKeyboardActions() {
300: JMenuItem item = new JMenuItem();
301: item.setUI(menuItemUI);
302: menuItemUI.uninstallKeyboardActions();
303: assertEquals("RegisteredKeyStrokes", 0, item
304: .getRegisteredKeyStrokes().length);
305: menuItemUI.installKeyboardActions();
306: assertEquals(14, item.getActionMap().allKeys().length);
307: if (!isHarmony()) {
308: assertEquals(1,
309: item.getActionMap().getParent().keys().length);
310: }
311: menuItemUI.uninstallKeyboardActions();
312: if (isHarmony()) {
313: assertEquals(0, item.getActionMap().allKeys().length);
314: } else {
315: assertNull(item.getActionMap().allKeys());
316: }
317: }
318:
319: /*
320: * Test method for 'javax.swing.plaf.basic.BasicMenuItemUI.createMouseInputListener(JComponent)'
321: */
322: public void testCreateMouseInputListener() {
323: assertNotNull(menuItemUI.createMouseInputListener(null));
324: }
325:
326: /*
327: * Test method for 'javax.swing.plaf.basic.BasicMenuItemUI.createMenuDragMouseListener(JComponent)'
328: */
329: public void testCreateMenuDragMouseListener() {
330: assertNotNull(menuItemUI.createMenuDragMouseListener(null));
331: }
332:
333: /*
334: * Test method for 'javax.swing.plaf.basic.BasicMenuItemUI.createMenuKeyListener(JComponent)'
335: */
336: public void testCreateMenuKeyListener() {
337: if (isHarmony()) {
338: assertNull(menuItemUI.createMenuKeyListener(null));
339: } else {
340: assertNotNull(menuItemUI.createMenuKeyListener(null));
341: }
342: }
343:
344: /*
345: * Test method for 'javax.swing.plaf.basic.BasicMenuItemUI.getPreferredMenuItemSize(JComponent, Icon, Icon, int)'
346: */
347: public void testGetPreferredMenuItemSize() {
348: Icon icon1 = null;
349: Icon icon2 = new ImageIcon(new BufferedImage(10, 20,
350: BufferedImage.TYPE_INT_RGB));
351: Icon icon3 = new ImageIcon(new BufferedImage(100, 20,
352: BufferedImage.TYPE_INT_RGB));
353: Icon icon4 = new ImageIcon(new BufferedImage(1000, 20,
354: BufferedImage.TYPE_INT_RGB));
355: JMenuItem item = new JMenuItem() {
356: private static final long serialVersionUID = 1L;
357:
358: @SuppressWarnings("deprecation")
359: @Override
360: public FontMetrics getFontMetrics(Font font) {
361: return Toolkit.getDefaultToolkit().getFontMetrics(font);
362: }
363: };
364: Border border = new BorderUIResource(BorderFactory
365: .createEmptyBorder(10, 20, 30, 40));
366: UIManager.put("MenuItem.border", border);
367: Insets margin = new InsetsUIResource(1, 2, 3, 4);
368: UIManager.put("OptionPane.margin", margin);
369: menuItemUI = new BasicMenuItemUI();
370: item.setUI(menuItemUI);
371: assertEquals(new Dimension(61, 41), menuItemUI
372: .getPreferredMenuItemSize(item, icon1, icon1, 0));
373: assertEquals(new Dimension(71, 41), menuItemUI
374: .getPreferredMenuItemSize(item, icon1, icon2, 0));
375: assertEquals(new Dimension(171, 41), menuItemUI
376: .getPreferredMenuItemSize(item, icon2, icon3, 0));
377: assertEquals(new Dimension(211, 41), menuItemUI
378: .getPreferredMenuItemSize(item, icon2, icon3, 10));
379: assertEquals(new Dimension(251, 41), menuItemUI
380: .getPreferredMenuItemSize(item, icon2, icon3, 20));
381: item.setIcon(icon4);
382: assertEquals(new Dimension(1061, 61), menuItemUI
383: .getPreferredMenuItemSize(item, icon1, icon1, 0));
384: assertEquals(new Dimension(1171, 61), menuItemUI
385: .getPreferredMenuItemSize(item, icon2, icon3, 0));
386: }
387:
388: /*
389: * Test method for 'javax.swing.plaf.basic.BasicMenuItemUI.getPath()'
390: */
391: @SuppressWarnings("deprecation")
392: public void testGetPath() {
393: JFrame frame = new JFrame();
394: JMenuBar menuBar = new JMenuBar();
395: JMenu menu = new JMenu("menu");
396: JMenu menu2 = new JMenu("menu");
397: JMenuItem item = new JMenuItem("item");
398: JMenuItem item2 = new JMenuItem("item 2");
399: item.setUI(menuItemUI);
400: menuBar.add(menu);
401: menuBar.add(menu2);
402: menu.add(item);
403: frame.getContentPane().add(menuBar);
404: frame.pack();
405: frame.show();
406: assertEquals(0, menuItemUI.getPath().length);
407: MenuSelectionManager manager = MenuSelectionManager
408: .defaultManager();
409: manager.setSelectedPath(new MenuElement[] { menuBar, menu,
410: menu.getPopupMenu(), item });
411: assertEquals(4, menuItemUI.getPath().length);
412: assertSame(menuBar, menuItemUI.getPath()[0]);
413: assertSame(menu, menuItemUI.getPath()[1]);
414: assertSame(menu.getPopupMenu(), menuItemUI.getPath()[2]);
415: assertSame(item, menuItemUI.getPath()[3]);
416: manager.setSelectedPath(new MenuElement[] { menuBar, item2,
417: menu2, menu.getPopupMenu() });
418: assertEquals(5, menuItemUI.getPath().length);
419: assertSame(menuBar, menuItemUI.getPath()[0]);
420: assertSame(item2, menuItemUI.getPath()[1]);
421: assertSame(menu2, menuItemUI.getPath()[2]);
422: assertSame(menu.getPopupMenu(), menuItemUI.getPath()[3]);
423: assertSame(item, menuItemUI.getPath()[4]);
424: manager.setSelectedPath(new MenuElement[] { menuBar });
425: assertEquals(1, menuItemUI.getPath().length);
426: assertSame(item, menuItemUI.getPath()[0]);
427: manager.clearSelectedPath();
428: assertEquals(0, menuItemUI.getPath().length);
429: frame.dispose();
430: }
431:
432: /*
433: * Test method for 'javax.swing.plaf.basic.BasicMenuItemUI.doClick(MenuSelectionManager)'
434: */
435: public void testDoClick() {
436: class MyJMenuItem extends JMenuItem {
437: private static final long serialVersionUID = 1L;
438:
439: public int time = -111;
440:
441: @Override
442: public void doClick(final int pressTime) {
443: time = pressTime;
444: }
445: }
446: MenuSelectionManager manager = new MenuSelectionManager();
447: manager.setSelectedPath(new MenuElement[] { new JMenu(),
448: new JMenu() });
449: MyJMenuItem item = new MyJMenuItem();
450: item.setUI(menuItemUI);
451: menuItemUI.doClick(manager);
452: assertEquals(0, manager.getSelectedPath().length);
453: assertEquals(0, item.time);
454: }
455: }
|