--- /dev/null 2017-12-08 19:11:15.000000000 +0530 +++ new/test/jdk/java/awt/LightweightComponent/LightWeightComponentPopupMenu/LightWeightComponentPopupMenu.java 2017-12-08 19:11:14.000000000 +0530 @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test 8189253 + * @key headful + * @summary JPopupMenu is inadvertently shown when using setComponentPopupMenu + * @compile LightWeightComponentPopupMenu.java + * @run main/manual LightWeightComponentPopupMenu + */ +import javax.swing.JDialog; +import javax.swing.JFrame; +import javax.swing.JMenuItem; +import javax.swing.JPopupMenu; +import javax.swing.JTextArea; +import javax.swing.JButton; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class LightWeightComponentPopupMenu { + private static JButton passButton; + private static JButton failButton; + private static JTextArea instructions; + private static JFrame mainFrame; + private static JFrame instructionFrame; + public static boolean isProgInterruption = false; + static Thread mainThread = null; + static int sleepTime = 300000; + + public LightWeightComponentPopupMenu() { + passButton = new JButton("Pass"); + passButton.setEnabled(true); + + failButton = new JButton("Fail"); + failButton.setEnabled(true); + + instructions = new JTextArea(8, 30); + instructions.setText(" This is a manual test\n\n" + + " 1) Right click on the window titled Main, a popup menu should be displayed\n" + + " 2) Move mouse to the dialog titled Modeless & right click on it\n" + + " 3) Press Pass if popup menu is not displayed when clicked on the Modeless dialog,\n" + + " 4) Press Fail otherwise"); + + instructionFrame = new JFrame("Test Instructions"); + instructionFrame.add(passButton); + instructionFrame.add(failButton); + instructionFrame.add(instructions); + instructionFrame.setSize(200,200); + instructionFrame.setLayout(new FlowLayout()); + instructionFrame.pack(); + instructionFrame.setVisible(true); + + passButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent ae) { + dispose(); + isProgInterruption = true; + mainThread.interrupt(); + } + }); + + failButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent ae) { + dispose(); + isProgInterruption = true; + mainThread.interrupt(); + throw new RuntimeException("Popup menu is shown on the modeless dialog!"); + } + }); + } + + private static void dispose() { + mainFrame.dispose(); + instructionFrame.dispose(); + } + private static void createAndShowPopupMenu() { + JPopupMenu popupMenu = new JPopupMenu(); + popupMenu.add(new JMenuItem("Popup menu")); + + mainFrame = new JFrame("Main"); + mainFrame.getRootPane().setComponentPopupMenu(popupMenu); + mainFrame.setSize(400, 400); + mainFrame.setLocationRelativeTo(null); + mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + mainFrame.setVisible(true); + + JTextArea info = new JTextArea(""); + info.setWrapStyleWord(true); + info.setLineWrap(true); + + JDialog dialog = new JDialog(mainFrame, "Modeless", false); + dialog.getContentPane().add(info); + dialog.setSize(300, 300); + dialog.setLocationRelativeTo(mainFrame); + dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + dialog.setVisible(true); + + mainFrame.toFront(); + } + + public static void main(String args[]) throws Exception { + mainThread = Thread.currentThread(); + LightWeightComponentPopupMenu test = new LightWeightComponentPopupMenu(); + test.createAndShowPopupMenu(); + + try { + mainThread.sleep(sleepTime); + } catch (InterruptedException e) { + if (!isProgInterruption) { + throw e; + } + } finally { + test.dispose(); + } + + if (!isProgInterruption) { + throw new RuntimeException("Timed out after " + sleepTime / 1000 + + " seconds"); + } + } +} +