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.lang.reflect.InvocationTargetException;
023: import javax.swing.BasicSwingTestCase;
024: import javax.swing.JButton;
025: import javax.swing.JFrame;
026: import javax.swing.JMenu;
027: import javax.swing.JMenuBar;
028: import javax.swing.JMenuItem;
029: import javax.swing.MenuElement;
030: import javax.swing.MenuSelectionManager;
031: import javax.swing.SwingUtilities;
032: import javax.swing.SwingWaitTestCase;
033: import javax.swing.event.PopupMenuEvent;
034: import javax.swing.event.PopupMenuListener;
035:
036: public class BasicMenuUI_MultithreadedTest extends BasicSwingTestCase {
037: protected BasicMenuUI menuUI;
038:
039: private class ConcretePopupListener implements PopupMenuListener {
040: public boolean visible;
041:
042: public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
043: visible = true;
044: synchronized (this ) {
045: notify();
046: }
047: }
048:
049: public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
050: }
051:
052: public void popupMenuCanceled(PopupMenuEvent e) {
053: }
054: }
055:
056: @Override
057: protected void setUp() throws Exception {
058: super .setUp();
059: menuUI = new BasicMenuUI();
060: }
061:
062: @Override
063: protected void tearDown() throws Exception {
064: menuUI = null;
065: super .tearDown();
066: }
067:
068: /*
069: * Test method for 'javax.swing.plaf.basic.BasicMenuUI.setupPostTimer(JMenu)'
070: */
071: @SuppressWarnings("deprecation")
072: public void testSetupPostTimer() throws InterruptedException,
073: InvocationTargetException {
074: final MenuSelectionManager manager = MenuSelectionManager
075: .defaultManager();
076: JFrame frame = new JFrame();
077: JMenuBar menuBar = new JMenuBar();
078: final JMenu menu = new JMenu("menu");
079: menu.setUI(menuUI);
080: ConcretePopupListener listener = new ConcretePopupListener();
081: menu.getPopupMenu().addPopupMenuListener(listener);
082: menu.add(new JMenuItem("item"));
083: frame.setJMenuBar(menuBar);
084: menuBar.add(menu);
085: final JButton button = new JButton();
086: frame.getContentPane().add(button);
087: frame.pack();
088: frame.show();
089: SwingWaitTestCase.requestFocusInWindowForComponent(button);
090: manager.setSelectedPath(new MenuElement[] { menuBar });
091: menu.setDelay(100);
092: setupTimerAntWait(menu, menuUI, listener);
093: assertFalse(listener.visible);
094: manager.setSelectedPath(new MenuElement[] { menuBar, menu });
095: setupTimerAntWait(menu, menuUI, listener);
096: assertTrue(listener.visible);
097: manager.setSelectedPath(new MenuElement[] { menuBar });
098: manager.setSelectedPath(new MenuElement[] { menuBar, menu });
099: menu.setDelay(10000);
100: setupTimerAntWait(menu, menuUI, listener);
101: assertFalse(listener.visible);
102: }
103:
104: private void setupTimerAntWait(final JMenu menu,
105: final BasicMenuUI ui, final ConcretePopupListener listener)
106: throws InterruptedException, InvocationTargetException {
107: synchronized (listener) {
108: listener.visible = false;
109: }
110: SwingUtilities.invokeAndWait(new Runnable() {
111: public void run() {
112: ui.setupPostTimer(menu);
113: }
114: });
115: synchronized (listener) {
116: listener.wait(1000);
117: }
118: }
119: }
|