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.Insets;
026: import java.awt.image.BufferedImage;
027: import javax.swing.BorderFactory;
028: import javax.swing.Icon;
029: import javax.swing.ImageIcon;
030: import javax.swing.JMenu;
031: import javax.swing.JMenuBar;
032: import javax.swing.JOptionPane;
033: import javax.swing.SwingTestCase;
034: import javax.swing.SwingUtilities;
035: import javax.swing.UIManager;
036: import javax.swing.border.Border;
037: import javax.swing.plaf.BorderUIResource;
038: import javax.swing.plaf.ColorUIResource;
039: import javax.swing.plaf.ComponentUI;
040: import javax.swing.plaf.FontUIResource;
041: import javax.swing.plaf.InsetsUIResource;
042:
043: public class BasicMenuUITest extends SwingTestCase {
044: protected BasicMenuUI menuUI;
045:
046: private Icon oldArrow;
047:
048: private Icon oldCheck;
049:
050: @Override
051: protected void setUp() throws Exception {
052: super .setUp();
053: menuUI = new BasicMenuUI();
054: oldArrow = UIManager.getIcon("Menu.arrowIcon");
055: oldCheck = UIManager.getIcon("Menu.checkIcon");
056: }
057:
058: @Override
059: protected void tearDown() throws Exception {
060: menuUI = null;
061: UIManager.put("Menu.arrowIcon", oldArrow);
062: UIManager.put("Menu.checkIcon", oldCheck);
063: super .tearDown();
064: }
065:
066: /*
067: * Test method for 'javax.swing.plaf.basic.BasicMenuUI.getMaximumSize(JComponent)'
068: */
069: public void testGetMaximumSize() {
070: JMenuBar bar = new JMenuBar();
071: JMenu menu1 = new JMenu();
072: menu1.setUI(menuUI);
073: assertNull(menuUI.getMaximumSize(menu1));
074: bar.add(menu1);
075: assertEquals(menuUI.getPreferredSize(menu1).width, menuUI
076: .getMaximumSize(menu1).width);
077: assertEquals(Short.MAX_VALUE,
078: menuUI.getMaximumSize(menu1).height);
079: menu1.setPreferredSize(new Dimension(1000, 1000));
080: assertEquals(1000, menuUI.getMaximumSize(menu1).width);
081: assertEquals(Short.MAX_VALUE,
082: menuUI.getMaximumSize(menu1).height);
083: testExceptionalCase(new NullPointerCase() {
084: @Override
085: public void exceptionalAction() throws Exception {
086: new BasicMenuUI().getMaximumSize(null);
087: }
088: });
089: testExceptionalCase(new NullPointerCase() {
090: @Override
091: // Regression for HARMONY-2663
092: public void exceptionalAction() throws Exception {
093: new BasicMenuUI().getMaximumSize(new JOptionPane());
094: }
095: });
096: }
097:
098: /*
099: * Test method for 'javax.swing.plaf.basic.BasicMenuUI.createUI(JComponent)'
100: */
101: public void testCreateUI() {
102: ComponentUI ui1 = BasicMenuUI.createUI(null);
103: ComponentUI ui2 = BasicMenuUI.createUI(null);
104: assertTrue(ui1 instanceof BasicMenuUI);
105: assertNotSame(ui1, ui2);
106: }
107:
108: /*
109: * Test method for 'javax.swing.plaf.basic.BasicMenuUI.getPropertyPrefix()'
110: */
111: public void testGetPropertyPrefix() {
112: assertEquals("Menu", menuUI.getPropertyPrefix());
113: }
114:
115: /*
116: * Test method for 'javax.swing.plaf.basic.BasicMenuUI.installDefaults()'
117: */
118: public void testInstallUninstallDefaults() {
119: JMenu menu = new JMenu();
120: UIManager.getDefaults().put("Menu.background",
121: new ColorUIResource(Color.red));
122: UIManager.getDefaults().put("Menu.foreground",
123: new ColorUIResource(Color.yellow));
124: UIManager.getDefaults().put("Menu.acceleratorForeground",
125: new ColorUIResource(Color.cyan));
126: UIManager.getDefaults().put(
127: "Menu.acceleratorSelectionForeground",
128: new ColorUIResource(Color.magenta));
129: UIManager.getDefaults().put("Menu.selectionBackground",
130: new ColorUIResource(Color.green));
131: UIManager.getDefaults().put("Menu.selectionForeground",
132: new ColorUIResource(Color.pink));
133: UIManager.getDefaults().put("Menu.disabledForeground",
134: new ColorUIResource(Color.orange));
135: Font font = new FontUIResource(menu.getFont().deriveFont(100f));
136: UIManager.getDefaults().put("Menu.font", font);
137: Border border = new BorderUIResource(BorderFactory
138: .createEmptyBorder(0, 0, 0, 0));
139: UIManager.getDefaults().put("Menu.border", border);
140: Insets margin = new InsetsUIResource(1, 2, 3, 4);
141: UIManager.getDefaults().put("Menu.margin", margin);
142: Icon arrow = new ImageIcon(new BufferedImage(10, 10,
143: BufferedImage.TYPE_INT_RGB));
144: UIManager.getDefaults().put("Menu.arrowIcon", arrow);
145: Icon check = new ImageIcon(new BufferedImage(10, 10,
146: BufferedImage.TYPE_INT_RGB));
147: UIManager.getDefaults().put("Menu.checkIcon", check);
148: menu.setUI(menuUI);
149: menuUI.installDefaults();
150: assertEquals(Color.red, menu.getBackground());
151: assertEquals(Color.yellow, menu.getForeground());
152: assertEquals(font, menu.getFont());
153: assertEquals(border, menu.getBorder());
154: assertEquals(margin, menu.getMargin());
155: assertEquals(4, menuUI.defaultTextIconGap);
156: assertTrue(menuUI.oldBorderPainted);
157: assertEquals(Color.cyan, menuUI.acceleratorForeground);
158: assertEquals(Color.magenta,
159: menuUI.acceleratorSelectionForeground);
160: assertEquals(Color.green, menuUI.selectionBackground);
161: assertEquals(Color.pink, menuUI.selectionForeground);
162: assertEquals(Color.orange, menuUI.disabledForeground);
163: assertEquals(arrow, menuUI.arrowIcon);
164: assertEquals(check, menuUI.checkIcon);
165: if (isHarmony()) {
166: assertFalse(menu.isOpaque());
167: }
168: menuUI.uninstallDefaults();
169: assertNull(menu.getBackground());
170: assertNull(menu.getForeground());
171: assertNull(menu.getFont());
172: assertNull(menu.getBorder());
173: // Assertion below has been deleted because now we are compartible with
174: // RI in this point. For details see HARMONY-4655
175: // assertNull(menu.getMargin());
176: assertEquals(4, menuUI.defaultTextIconGap);
177: assertTrue(menuUI.oldBorderPainted);
178: assertEquals(Color.cyan, menuUI.acceleratorForeground);
179: assertEquals(Color.magenta,
180: menuUI.acceleratorSelectionForeground);
181: assertEquals(Color.green, menuUI.selectionBackground);
182: assertEquals(Color.pink, menuUI.selectionForeground);
183: assertEquals(Color.orange, menuUI.disabledForeground);
184: assertEquals(arrow, menuUI.arrowIcon);
185: assertEquals(check, menuUI.checkIcon);
186: }
187:
188: /*
189: * Test method for 'javax.swing.plaf.basic.BasicMenuUI.installListeners()'
190: */
191: public void testInstallUninstallListeners() {
192: JMenu menu = new JMenu();
193: menuUI = (BasicMenuUI) menu.getUI();
194: menuUI.uninstallListeners();
195: assertEquals(0, menu.getMenuDragMouseListeners().length);
196: assertEquals(0, menu.getMenuKeyListeners().length);
197: assertEquals(0, menu.getPropertyChangeListeners().length);
198: assertEquals(0, menu.getMouseMotionListeners().length);
199: assertEquals(0, menu.getMouseListeners().length);
200: assertEquals(0, menu.getChangeListeners().length);
201: assertNull(menuUI.mouseInputListener);
202: assertNull(menuUI.menuKeyListener);
203: assertNull(menuUI.menuDragMouseListener);
204: menuUI.menuItem = menu;
205: menuUI.installListeners();
206: assertNull(menuUI.changeListener);
207: assertNull(menuUI.menuListener);
208: assertNotNull(menuUI.propertyChangeListener);
209: assertNotNull(menuUI.mouseInputListener);
210: if (!isHarmony()) {
211: assertNotNull(menuUI.menuKeyListener);
212: }
213: assertNotNull(menuUI.menuDragMouseListener);
214: assertSame(menuUI.menuDragMouseListener, menu
215: .getMenuDragMouseListeners()[0]);
216: if (!isHarmony()) {
217: assertSame(menuUI.menuKeyListener, menu
218: .getMenuKeyListeners()[0]);
219: }
220: assertSame(menuUI.propertyChangeListener, menu
221: .getPropertyChangeListeners()[0]);
222: assertSame(menuUI.mouseInputListener, menu
223: .getMouseMotionListeners()[0]);
224: assertSame(menuUI.mouseInputListener,
225: menu.getMouseListeners()[0]);
226: menuUI.uninstallListeners();
227: assertNull(menuUI.propertyChangeListener);
228: assertNull(menuUI.mouseInputListener);
229: assertNull(menuUI.menuKeyListener);
230: assertNull(menuUI.menuDragMouseListener);
231: assertEquals(0, menu.getMenuDragMouseListeners().length);
232: assertEquals(0, menu.getMenuKeyListeners().length);
233: assertEquals(0, menu.getPropertyChangeListeners().length);
234: assertEquals(0, menu.getMouseMotionListeners().length);
235: assertEquals(0, menu.getMouseListeners().length);
236: }
237:
238: /*
239: * Test method for 'javax.swing.plaf.basic.BasicMenuUI.installKeyboardActions()'
240: */
241: public void testInstallUninstallKeyboardActions() {
242: JMenu menu = new JMenu();
243: menu.setUI(menuUI);
244: menuUI.uninstallKeyboardActions();
245: assertEquals("RegisteredKeyStrokes", 0, menu
246: .getRegisteredKeyStrokes().length);
247: menuUI.installKeyboardActions();
248: if (isHarmony()) {
249: assertEquals(14, menu.getActionMap().allKeys().length);
250: assertEquals(1,
251: menu.getActionMap().getParent().keys().length);
252: } else {
253: assertEquals(15, menu.getActionMap().allKeys().length);
254: assertEquals(2,
255: menu.getActionMap().getParent().keys().length);
256: }
257: menuUI.uninstallKeyboardActions();
258: if (isHarmony()) {
259: assertEquals(0, menu.getActionMap().allKeys().length);
260: } else {
261: assertNull(menu.getActionMap().allKeys());
262: }
263: }
264:
265: /*
266: * Test method for 'javax.swing.plaf.basic.BasicMenuUI.createMouseInputListener(JComponent)'
267: */
268: public void testCreateMouseInputListener() {
269: assertNotNull(menuUI.createMouseInputListener(null));
270: }
271:
272: /*
273: * Test method for 'javax.swing.plaf.basic.BasicMenuUI.createMenuDragMouseListener(JComponent)'
274: */
275: public void testCreateMenuDragMouseListener() {
276: assertNotNull(menuUI.createMenuDragMouseListener(null));
277: if (isHarmony()) {
278: assertSame(menuUI.createMenuDragMouseListener(null), menuUI
279: .createMenuDragMouseListener(null));
280: }
281: }
282:
283: /*
284: * Test method for 'javax.swing.plaf.basic.BasicMenuUI.createMenuKeyListener(JComponent)'
285: */
286: public void testCreateMenuKeyListener() {
287: if (isHarmony()) {
288: assertNull(menuUI.createMenuKeyListener(null));
289: assertSame(menuUI.createMenuKeyListener(null), menuUI
290: .createMenuKeyListener(null));
291: } else {
292: assertNotNull(menuUI.createMenuKeyListener(null));
293: }
294: }
295:
296: /*
297: * Test method for 'javax.swing.plaf.basic.BasicMenuUI.BasicMenuUI()'
298: */
299: public void testBasicMenuUI() {
300: assertNull(menuUI.changeListener);
301: assertNull(menuUI.menuListener);
302: assertNull(menuUI.propertyChangeListener);
303: }
304:
305: /*
306: * Test method for 'javax.swing.plaf.basic.BasicMenuUI.createChangeListener(JComponent)'
307: */
308: public void testCreateChangeListener() {
309: assertNull(menuUI.createChangeListener(null));
310: assertNull(menuUI.createChangeListener(new JMenu()));
311: }
312:
313: /*
314: * Test method for 'javax.swing.plaf.basic.BasicMenuUI.createMenuListener(JComponent)'
315: */
316: public void testCreateMenuListener() {
317: if (!isHarmony()) {
318: return;
319: }
320: // Updated for regression of HARMONY-2663
321: assertNull(menuUI.createMenuListener(null));
322: assertNull(menuUI.createMenuListener(new JMenu()));
323: }
324:
325: /*
326: * Test method for 'javax.swing.plaf.basic.BasicMenuUI.createPropertyChangeListener(JComponent)'
327: */
328: public void testCreatePropertyChangeListener() {
329: assertNotNull(menuUI.createPropertyChangeListener(null));
330: }
331:
332: public void testInstallUninstallUI() {
333: JMenu item = new JMenu();
334: menuUI.installUI(item);
335: assertNotNull(item.getBorder());
336: assertNotNull(SwingUtilities.getUIActionMap(item));
337: assertNotNull(menuUI.mouseInputListener);
338: assertEquals(0, item.getComponentCount());
339: menuUI.uninstallUI(item);
340: assertNull(item.getBorder());
341: assertNull(SwingUtilities.getUIActionMap(item));
342: assertNull(menuUI.mouseInputListener);
343: assertEquals(0, item.getComponentCount());
344: }
345: }
|