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: package javax.swing.plaf.basic;
018:
019: import java.awt.Color;
020: import java.awt.Component;
021: import java.awt.ComponentOrientation;
022: import java.awt.EventQueue;
023: import java.awt.Font;
024: import java.awt.LayoutManager;
025: import java.awt.event.InputEvent;
026: import java.awt.event.MouseEvent;
027: import javax.swing.BorderFactory;
028: import javax.swing.JPanel;
029: import javax.swing.JPopupMenu;
030: import javax.swing.OverlayLayout;
031: import javax.swing.UIManager;
032: import javax.swing.border.Border;
033: import javax.swing.plaf.BorderUIResource;
034: import javax.swing.plaf.ColorUIResource;
035: import javax.swing.plaf.ComponentUI;
036: import javax.swing.plaf.FontUIResource;
037: import javax.swing.plaf.PopupMenuUITest;
038:
039: public class BasicPopupMenuUITest extends PopupMenuUITest {
040: protected BasicPopupMenuUI basicPopupUI;
041:
042: @Override
043: protected void setUp() throws Exception {
044: super .setUp();
045: basicPopupUI = new BasicPopupMenuUI();
046: popupUI = basicPopupUI;
047: }
048:
049: @Override
050: protected void tearDown() throws Exception {
051: basicPopupUI = null;
052: super .tearDown();
053: }
054:
055: /*
056: * Test method for 'javax.swing.plaf.basic.BasicPopupMenuUI.createUI(JComponent)'
057: */
058: public void testCreateUI() {
059: JPanel panel = new JPanel();
060: ComponentUI ui1 = BasicPopupMenuUI.createUI(null);
061: BasicPopupMenuUI ui2 = (BasicPopupMenuUI) BasicPopupMenuUI
062: .createUI(panel);
063: assertTrue(ui1 instanceof BasicPopupMenuUI);
064: assertNotSame(ui1, ui2);
065: }
066:
067: /*
068: * Test method for 'javax.swing.plaf.PopupMenuUI.isPopupTrigger(MouseEvent)'
069: */
070: @Override
071: public void testIsPopupTrigger() {
072: Component source1 = new JPanel();
073: JPopupMenu source2 = new JPopupMenu();
074: MouseEvent event1 = new MouseEvent(source1,
075: MouseEvent.MOUSE_ENTERED, EventQueue
076: .getMostRecentEventTime(), 0, 5, 5, 0, false);
077: MouseEvent event2 = new MouseEvent(source1,
078: MouseEvent.MOUSE_WHEEL, EventQueue
079: .getMostRecentEventTime(), 0, 5, 5, 0, true);
080: MouseEvent event3 = new MouseEvent(source1,
081: MouseEvent.MOUSE_PRESSED, EventQueue
082: .getMostRecentEventTime(), 0, 5, 5, 0, true);
083: MouseEvent event4 = new MouseEvent(source2,
084: MouseEvent.MOUSE_RELEASED, EventQueue
085: .getMostRecentEventTime(),
086: InputEvent.BUTTON1_DOWN_MASK, 1, 1, 1, true);
087: source2.setUI(popupUI);
088: assertFalse(popupUI.isPopupTrigger(event1));
089: assertFalse(popupUI.isPopupTrigger(event2));
090: assertFalse(popupUI.isPopupTrigger(event3));
091: assertFalse(popupUI.isPopupTrigger(event4));
092:
093: // regression for HARMONY-2512
094: try {
095: BasicPopupMenuUI m = new BasicPopupMenuUI();
096:
097: m.isPopupTrigger(null);
098: fail("NPE should be thrown");
099: } catch (NullPointerException npe) {
100: // PASSED
101: }
102: }
103:
104: /*
105: * Test method for 'javax.swing.plaf.basic.BasicPopupMenuUI.installDefaults()'
106: */
107: public void testInstallUninstallUI() {
108: JPopupMenu menu = new JPopupMenu();
109: menu.setUI(basicPopupUI);
110: LayoutManager oldManager = menu.getLayout();
111: basicPopupUI.uninstallUI(menu);
112: basicPopupUI.installUI(menu);
113: assertNotNull(menu.getLayout());
114: assertNotSame(oldManager, menu.getLayout());
115: basicPopupUI.uninstallUI(menu);
116: oldManager = new OverlayLayout(menu);
117: menu.setLayout(oldManager);
118: basicPopupUI.installUI(menu);
119: assertEquals(oldManager, menu.getLayout());
120: }
121:
122: /*
123: * Test method for 'javax.swing.plaf.basic.BasicPopupMenuUI.installDefaults()'
124: */
125: public void testInstallUninstallDefaults() {
126: JPopupMenu menu = new JPopupMenu();
127: UIManager.getDefaults().put("PopupMenu.background",
128: new ColorUIResource(Color.red));
129: UIManager.getDefaults().put("PopupMenu.foreground",
130: new ColorUIResource(Color.yellow));
131: Font font = new FontUIResource(menu.getFont().deriveFont(100f));
132: UIManager.getDefaults().put("PopupMenu.font", font);
133: Border border = new BorderUIResource(BorderFactory
134: .createEmptyBorder(0, 0, 0, 0));
135: UIManager.getDefaults().put("PopupMenu.border", border);
136: menu.setUI(basicPopupUI);
137: basicPopupUI.installDefaults();
138: assertEquals(Color.red, menu.getBackground());
139: assertEquals(Color.yellow, menu.getForeground());
140: assertEquals(font, menu.getFont());
141: assertEquals(border, menu.getBorder());
142: assertTrue(menu.isOpaque());
143: basicPopupUI.uninstallDefaults();
144: assertNull(menu.getBackground());
145: assertNull(menu.getForeground());
146: assertNull(menu.getFont());
147: assertNull(menu.getBorder());
148: }
149:
150: /*
151: * Test method for 'javax.swing.plaf.basic.BasicPopupMenuUI.installKeyboardActions()'
152: */
153: public void testInstallUninstallKeyboardActions() {
154: if (!isHarmony()) {
155: return;
156: }
157: JPopupMenu menu = new JPopupMenu();
158: menu.setUI(null);
159: basicPopupUI.popupMenu = menu;
160: menu
161: .applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
162: basicPopupUI.installKeyboardActions();
163: assertEquals(0, menu.getInputMap().keys().length);
164: assertNotNull(menu.getInputMap().getParent());
165: assertEquals(11, menu.getInputMap().getParent().keys().length);
166: assertNull(menu.getInputMap().getParent().getParent());
167: basicPopupUI.uninstallKeyboardActions();
168: assertEquals(0, menu.getInputMap().allKeys().length);
169: menu
170: .applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
171: basicPopupUI.installKeyboardActions();
172: assertEquals(0, menu.getInputMap().keys().length);
173: assertNotNull(menu.getInputMap().getParent());
174: assertEquals(4, menu.getInputMap().getParent().keys().length);
175: assertNotNull(menu.getInputMap().getParent().getParent());
176: assertEquals(11, menu.getInputMap().getParent().getParent()
177: .keys().length);
178: assertNull(menu.getInputMap().getParent().getParent()
179: .getParent());
180: }
181:
182: /*
183: * Test method for 'javax.swing.plaf.basic.BasicPopupMenuUI.installListeners()'
184: */
185: public void testInstallUninstallListeners() {
186: JPopupMenu menu = new JPopupMenu();
187: menu.setUI(null);
188: basicPopupUI.popupMenu = menu;
189: basicPopupUI.installListeners();
190: if (!isHarmony()) {
191: assertEquals(1, menu.getPopupMenuListeners().length);
192: }
193: basicPopupUI.uninstallListeners();
194: if (!isHarmony()) {
195: assertEquals(0, menu.getPopupMenuListeners().length);
196: }
197: }
198:
199: /**
200: * Regression test for HARMONY-2654
201: * */
202: public void testInstallKeyboardActions()
203: throws NullPointerException {
204: BasicPopupMenuUIForTest localBasicPopupMenuUI = new BasicPopupMenuUIForTest();
205: localBasicPopupMenuUI.installKeyboardActions();
206: }
207:
208: class BasicPopupMenuUIForTest extends BasicPopupMenuUI {
209: public void installKeyboardActions() {
210: super.installKeyboardActions();
211: }
212: }
213: }
|