Path: blob/master/test/jdk/javax/swing/JPopupMenu/8173739/TestPopupMenu.java
41153 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @key headful25* @bug 817373926* @summary Verifies if JPopupMenu disappears on KeyEvent27* @run main TestPopupMenu28*/29import java.awt.Color;30import java.awt.Dimension;31import java.awt.GridBagConstraints;32import java.awt.GridBagLayout;33import java.awt.Insets;34import java.awt.Point;35import java.awt.Robot;36import java.awt.event.InputEvent;37import java.awt.event.KeyEvent;38import java.beans.PropertyVetoException;39import javax.swing.JComponent;40import javax.swing.JDesktopPane;41import javax.swing.JFrame;42import javax.swing.JInternalFrame;43import javax.swing.JLabel;44import javax.swing.JMenuItem;45import javax.swing.JPanel;46import javax.swing.JPopupMenu;47import javax.swing.JScrollPane;48import javax.swing.KeyStroke;49import javax.swing.SwingUtilities;5051public class TestPopupMenu {52private JFrame frame;53private JLabel label;54private volatile Point p = null;55private volatile Dimension d = null;5657public static void main(String[] args) throws Exception {58new TestPopupMenu();59}6061void blockTillDisplayed(JComponent comp) throws Exception {62while (p == null) {63try {64SwingUtilities.invokeAndWait(() -> {65p = comp.getLocationOnScreen();66d = comp.getSize();67});68} catch (IllegalStateException e) {69try {70Thread.sleep(1000);71} catch (InterruptedException ie) {72}73}74}75}7677public TestPopupMenu() throws Exception {78Robot robot = new Robot();79robot.setAutoDelay(200);80try {81SwingUtilities.invokeAndWait(() -> {82try {83createAndShowUI();84} catch (Exception ex) {85throw new RuntimeException(ex);86}87});88blockTillDisplayed(label);89robot.waitForIdle();90robot.mouseMove(p.x + d.width/2, p.y + d.height/2);91robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);92robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);93robot.waitForIdle();94robot.keyPress(KeyEvent.VK_CONTROL);95robot.keyPress(KeyEvent.VK_U);96robot.keyRelease(KeyEvent.VK_U);97robot.keyRelease(KeyEvent.VK_CONTROL);98robot.waitForIdle();99JPopupMenu popup = label.getComponentPopupMenu();100if (popup != null && popup.isVisible()) {101throw new RuntimeException("Popup is visible in wrong internal frame");102}103} finally {104SwingUtilities.invokeAndWait(()->frame.dispose());105}106}107108private void createAndShowUI() throws Exception {109frame = new JFrame();110frame.setTitle("Test Frame");111frame.setSize(800, 600);112113JDesktopPane pane = new JDesktopPane();114TestInternalFrameWPopup testInternalFrame1 = new TestInternalFrameWPopup();115pane.add(testInternalFrame1);116117testInternalFrame1.setVisible(true);118JScrollPane scrollPane = new JScrollPane(pane);119frame.getContentPane().add(scrollPane);120testInternalFrame1.setMaximum(true);121frame.getRootPane().registerKeyboardAction(e -> {122TestInternalFrame testInternalFrame2 = new TestInternalFrame();123pane.add(testInternalFrame2);124try {125testInternalFrame2.setMaximum(true);126} catch (PropertyVetoException ex) {127throw new RuntimeException(ex);128}129testInternalFrame2.setVisible(true);130}, KeyStroke.getKeyStroke(KeyEvent.VK_U, KeyEvent.CTRL_MASK),131JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);132133frame.setVisible(true);134}135136/**137* Background color Cyan138*/139class TestInternalFrameWPopup extends JInternalFrame {140141TestInternalFrameWPopup() {142jbInit();143}144145private void jbInit() {146setTitle("Test Internal Frame With Popup");147setContentPane(getContainerPanel());148setMaximizable(true);149setClosable(true);150setMinimumSize(new Dimension(500, 300));151setSize(500, 300);152}153154private JPanel getContainerPanel() {155JPanel panel = new JPanel();156panel.setLayout(new GridBagLayout());157label = new JLabel("Test Label");158JPopupMenu popup = new JPopupMenu();159JMenuItem menuItem1 = new JMenuItem("Item 1");160JMenuItem menuItem2 = new JMenuItem("Item 2");161JMenuItem menuItem3 = new JMenuItem("Item 3");162JMenuItem menuItem4 = new JMenuItem("Item 4");163JMenuItem menuItem5 = new JMenuItem("Item 5");164menuItem1.setOpaque(false);165menuItem2.setOpaque(false);166menuItem3.setOpaque(false);167menuItem4.setOpaque(false);168menuItem5.setOpaque(false);169popup.add(menuItem1);170popup.add(menuItem2);171popup.add(menuItem3);172popup.add(menuItem4);173popup.add(menuItem5);174label.setComponentPopupMenu(popup);175popup.setBackground(Color.CYAN);176panel.add(label, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,177GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));178panel.setBackground(Color.CYAN);179return panel;180}181}182183/**184* Background color Gray185*186*/187class TestInternalFrame extends JInternalFrame {188public TestInternalFrame() {189jbInit();190}191192private void jbInit() {193setTitle("Test Internal Frame");194getContentPane().setBackground(Color.GRAY);195setMaximizable(true);196setClosable(true);197setMinimumSize(new Dimension(500, 300));198setSize(500, 300);199}200}201}202203204