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.Font;
024: import java.awt.LayoutManager;
025: import java.awt.event.ContainerListener;
026: import javax.swing.BorderFactory;
027: import javax.swing.JComponent;
028: import javax.swing.JMenu;
029: import javax.swing.JMenuBar;
030: import javax.swing.OverlayLayout;
031: import javax.swing.SwingTestCase;
032: import javax.swing.UIManager;
033: import javax.swing.border.Border;
034: import javax.swing.plaf.BorderUIResource;
035: import javax.swing.plaf.ColorUIResource;
036: import javax.swing.plaf.ComponentUI;
037: import javax.swing.plaf.FontUIResource;
038:
039: public class BasicMenuBarUITest extends SwingTestCase {
040: protected BasicMenuBarUI menuBarUI;
041:
042: @Override
043: protected void setUp() throws Exception {
044: super .setUp();
045: menuBarUI = new BasicMenuBarUI();
046: }
047:
048: @Override
049: protected void tearDown() throws Exception {
050: menuBarUI = null;
051: super .tearDown();
052: }
053:
054: /*
055: * Test method for 'javax.swing.plaf.basic.BasicMenuBarUI.installUI(JComponent)'
056: */
057: public void testInstallUninstallUI() {
058: JMenuBar menu = new JMenuBar();
059: menu.setUI(menuBarUI);
060: LayoutManager oldManager = menu.getLayout();
061: menuBarUI.uninstallUI(menu);
062: menuBarUI.installUI(menu);
063: assertNotNull(menu.getLayout());
064: assertNotSame(oldManager, menu.getLayout());
065: menuBarUI.uninstallUI(menu);
066: oldManager = new OverlayLayout(menu);
067: menu.setLayout(oldManager);
068: menuBarUI.installUI(menu);
069: assertEquals(oldManager, menu.getLayout());
070: }
071:
072: public void testInstallUninstallUI2() {
073: JMenuBar menu = new JMenuBar();
074: menu.setUI(menuBarUI);
075: menuBarUI.installUI(menu);
076: assertSame(menu, menuBarUI.menuBar);
077: menuBarUI.uninstallUI(menu);
078: assertNull(menuBarUI.menuBar);
079: }
080:
081: /*
082: * Test method for 'javax.swing.plaf.basic.BasicMenuBarUI.createUI(JComponent)'
083: */
084: public void testCreateUI() {
085: ComponentUI ui1 = BasicMenuBarUI.createUI(null);
086: ComponentUI ui2 = BasicMenuBarUI.createUI(null);
087: assertTrue(ui1 instanceof BasicMenuBarUI);
088: assertNotSame(ui1, ui2);
089: }
090:
091: /*
092: * Test method for 'javax.swing.plaf.basic.BasicMenuBarUI.createChangeListener()'
093: */
094: public void testCreateChangeListener() {
095: assertNotNull(menuBarUI.createChangeListener());
096: assertSame(menuBarUI.createChangeListener(), menuBarUI
097: .createChangeListener());
098: }
099:
100: /*
101: * Test method for 'javax.swing.plaf.basic.BasicMenuBarUI.createContainerListener()'
102: */
103: public void testCreateContainerListener() {
104: final ContainerListener containerListener1 = menuBarUI
105: .createContainerListener();
106: final ContainerListener containerListener2 = menuBarUI
107: .createContainerListener();
108: assertNotNull(containerListener1);
109: assertSame(containerListener1, containerListener2);
110: assertSame(containerListener1, menuBarUI.createChangeListener());
111: }
112:
113: /*
114: * Test method for 'javax.swing.plaf.basic.BasicMenuBarUI.installDefaults()'
115: */
116: public void testInstallUninstallDefaults() {
117: JMenuBar menuBar = new JMenuBar();
118: UIManager.getDefaults().put("MenuBar.background",
119: new ColorUIResource(Color.red));
120: UIManager.getDefaults().put("MenuBar.foreground",
121: new ColorUIResource(Color.yellow));
122: UIManager.getDefaults().put("MenuBar.highlight",
123: new ColorUIResource(Color.cyan));
124: UIManager.getDefaults().put("MenuBar.shadow",
125: new ColorUIResource(Color.magenta));
126: Font font = new FontUIResource(menuBar.getFont().deriveFont(
127: 100f));
128: UIManager.getDefaults().put("MenuBar.font", font);
129: Border border = new BorderUIResource(BorderFactory
130: .createEmptyBorder(0, 0, 0, 0));
131: UIManager.getDefaults().put("MenuBar.border", border);
132: menuBar.setUI(menuBarUI);
133: menuBarUI.installDefaults();
134: assertEquals(Color.red, menuBar.getBackground());
135: assertEquals(Color.yellow, menuBar.getForeground());
136: assertEquals(font, menuBar.getFont());
137: assertEquals(border, menuBar.getBorder());
138: menuBarUI.uninstallDefaults();
139: assertEquals(Color.red, menuBar.getBackground());
140: assertEquals(Color.yellow, menuBar.getForeground());
141: assertEquals(font, menuBar.getFont());
142: assertNull(menuBar.getBorder());
143: }
144:
145: /*
146: * Test method for 'javax.swing.plaf.basic.BasicMenuBarUI.installKeyboardActions()'
147: */
148: public void testInstallUninstallKeyboardActions() {
149: JMenuBar menuBar = new JMenuBar();
150: menuBar.setUI(menuBarUI);
151: menuBarUI.installKeyboardActions();
152: assertEquals(1, menuBar.getInputMap(
153: JComponent.WHEN_IN_FOCUSED_WINDOW).allKeys().length);
154: assertEquals(1, menuBar.getActionMap().allKeys().length);
155: menuBarUI.uninstallKeyboardActions();
156: if (isHarmony()) {
157: assertEquals(0, menuBar.getInputMap(
158: JComponent.WHEN_IN_FOCUSED_WINDOW).allKeys().length);
159: assertEquals(0, menuBar.getActionMap().allKeys().length);
160: } else {
161: assertNull(menuBar.getInputMap(
162: JComponent.WHEN_IN_FOCUSED_WINDOW).allKeys());
163: assertNull(menuBar.getActionMap().allKeys());
164: }
165: }
166:
167: /*
168: * Test method for 'javax.swing.plaf.basic.BasicMenuBarUI.installListeners()'
169: */
170: public void testInstallUninstallListeners() {
171: JMenuBar menuBar = new JMenuBar();
172: JMenu menu = new JMenu();
173: menuBar.add(menu);
174: menuBarUI = (BasicMenuBarUI) menuBar.getUI();
175: menuBarUI.uninstallListeners();
176: assertEquals(0, menuBar.getContainerListeners().length);
177: assertNull(menuBarUI.changeListener);
178: assertNull(menuBarUI.containerListener);
179: menuBarUI.menuBar = menuBar;
180: menuBarUI.installListeners();
181: assertEquals(1, menuBar.getContainerListeners().length);
182: assertNotNull(menuBarUI.changeListener);
183: assertNotNull(menuBarUI.containerListener);
184: }
185: }
|