|
| 1 | +/* |
| 2 | + * Copyright (c) 2002, 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import javax.swing.JFrame; |
| 25 | +import javax.swing.JMenuItem; |
| 26 | +import javax.swing.JPopupMenu; |
| 27 | +import javax.swing.SwingUtilities; |
| 28 | +import java.awt.Component; |
| 29 | +import java.awt.Dimension; |
| 30 | +import java.awt.GraphicsConfiguration; |
| 31 | +import java.awt.GraphicsDevice; |
| 32 | +import java.awt.GraphicsEnvironment; |
| 33 | +import java.awt.Insets; |
| 34 | +import java.awt.Robot; |
| 35 | +import java.awt.Window; |
| 36 | +import java.awt.Toolkit; |
| 37 | +import java.awt.event.ActionEvent; |
| 38 | +import java.awt.event.ActionListener; |
| 39 | +import java.awt.event.MouseAdapter; |
| 40 | +import java.awt.event.MouseEvent; |
| 41 | +import java.awt.event.InputEvent; |
| 42 | + |
| 43 | +/* |
| 44 | + * @test |
| 45 | + * @bug 4711693 |
| 46 | + * @summary Pop-up doesn't stay up |
| 47 | + * @key headful |
| 48 | + * @run main bug4711693 |
| 49 | + */ |
| 50 | + |
| 51 | +public class bug4711693 { |
| 52 | + static JFrame fr; |
| 53 | + static Robot robot; |
| 54 | + static volatile boolean passed = true; |
| 55 | + static volatile Dimension scr; |
| 56 | + |
| 57 | + public static void main(String[] args) throws Exception { |
| 58 | + try { |
| 59 | + robot = new Robot(); |
| 60 | + SwingUtilities.invokeAndWait(() -> { |
| 61 | + fr = new JFrame("Test 4711693"); |
| 62 | + scr = new Dimension(); |
| 63 | + fr.setSize(600, 600); |
| 64 | + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
| 65 | + GraphicsDevice[] gs = ge.getScreenDevices(); |
| 66 | + GraphicsConfiguration gc = null; |
| 67 | + |
| 68 | + for (int j = 0; j < gs.length; j++) { |
| 69 | + GraphicsDevice gd = gs[j]; |
| 70 | + gc = gd.getDefaultConfiguration(); |
| 71 | + if (gc.getBounds().contains(100, 100)) break; |
| 72 | + } |
| 73 | + scr = Toolkit.getDefaultToolkit().getScreenSize(); |
| 74 | + Insets ins = Toolkit.getDefaultToolkit().getScreenInsets(gc); |
| 75 | + scr.width -= ins.right; |
| 76 | + scr.height -= ins.bottom; |
| 77 | + fr.setLocation(scr.width - 400, scr.height - 400); |
| 78 | + fr.setVisible(true); |
| 79 | + }); |
| 80 | + robot.waitForIdle(); |
| 81 | + robot.delay(1000); |
| 82 | + SwingUtilities.invokeAndWait(() -> { |
| 83 | + final JPopupMenu popupMenu = new JPopupMenu(); |
| 84 | + final Component pane = fr.getContentPane(); |
| 85 | + for (int i = 1; i < 10; i++) { |
| 86 | + final String itemName = "Item " + i; |
| 87 | + JMenuItem it = popupMenu.add(new JMenuItem(itemName)); |
| 88 | + it.addActionListener(new ActionListener() { |
| 89 | + public void actionPerformed(ActionEvent a) { |
| 90 | + passed = false; |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + pane.addMouseListener(new MouseAdapter() { |
| 96 | + public void mousePressed(MouseEvent e) { |
| 97 | + if ((e.isAltDown() || |
| 98 | + ((e.getModifiersEx() & |
| 99 | + InputEvent.BUTTON3_DOWN_MASK) != 0))) { |
| 100 | + Component parent = e.getComponent(); |
| 101 | + while (parent != null && !(parent instanceof Window)) { |
| 102 | + parent = parent.getParent(); |
| 103 | + } |
| 104 | + popupMenu.show(pane, e.getX(), e.getY()); |
| 105 | + } |
| 106 | + } |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + robot.mouseMove(scr.width - 55, scr.height - 55); |
| 111 | + robot.mousePress(InputEvent.BUTTON3_DOWN_MASK); |
| 112 | + robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK); |
| 113 | + } finally { |
| 114 | + SwingUtilities.invokeAndWait(() -> { |
| 115 | + if (fr != null) { |
| 116 | + fr.dispose(); |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + if (!passed) { |
| 121 | + throw new RuntimeException("Test failed. Popup disposed on mouse release."); |
| 122 | + } else { |
| 123 | + System.out.println("Test Passed!"); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments