|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, 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 | +/* |
| 25 | + * @test |
| 26 | + * @summary tests if nested menu is displayed on Wayland |
| 27 | + * @key headful |
| 28 | + * @bug 8342096 |
| 29 | + * @requires (os.family == "linux") |
| 30 | + */ |
| 31 | + |
| 32 | +import javax.swing.JButton; |
| 33 | +import javax.swing.JFrame; |
| 34 | +import javax.swing.JMenu; |
| 35 | +import javax.swing.JPanel; |
| 36 | +import javax.swing.JPopupMenu; |
| 37 | +import javax.swing.SwingUtilities; |
| 38 | +import java.awt.Dimension; |
| 39 | +import java.awt.Point; |
| 40 | +import java.awt.Robot; |
| 41 | +import java.awt.event.InputEvent; |
| 42 | + |
| 43 | +public class NestedFocusablePopupTest { |
| 44 | + |
| 45 | + static volatile JMenu menu; |
| 46 | + static volatile JPopupMenu popupMenu; |
| 47 | + static volatile JFrame frame; |
| 48 | + |
| 49 | + public static void main(String[] args) throws Exception { |
| 50 | + if (System.getenv("WAYLAND_DISPLAY") == null) { |
| 51 | + //test is valid only when running on Wayland. |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + Robot robot = new Robot(); |
| 56 | + robot.setAutoDelay(50); |
| 57 | + |
| 58 | + try { |
| 59 | + SwingUtilities.invokeAndWait(NestedFocusablePopupTest::initAndShowGui); |
| 60 | + robot.waitForIdle(); |
| 61 | + robot.delay(500); |
| 62 | + |
| 63 | + Point frameLocation = frame.getLocationOnScreen(); |
| 64 | + robot.mouseMove(frameLocation.x + frame.getWidth() / 2, |
| 65 | + frameLocation.y + frame.getHeight() / 2); |
| 66 | + |
| 67 | + robot.mousePress(InputEvent.BUTTON3_DOWN_MASK); |
| 68 | + robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK); |
| 69 | + |
| 70 | + robot.waitForIdle(); |
| 71 | + robot.delay(100); |
| 72 | + |
| 73 | + Point menuLocation = menu.getLocationOnScreen(); |
| 74 | + robot.mouseMove(menuLocation.x + 5, menuLocation.y + 5); |
| 75 | + robot.waitForIdle(); |
| 76 | + robot.delay(200); |
| 77 | + |
| 78 | + if (!popupMenu.isVisible()) { |
| 79 | + throw new RuntimeException("Popup is not visible"); |
| 80 | + } |
| 81 | + } finally { |
| 82 | + SwingUtilities.invokeAndWait(frame::dispose); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + private static void initAndShowGui() { |
| 88 | + frame = new JFrame("NestedFocusablePopupTest"); |
| 89 | + JPanel panel = new JPanel(); |
| 90 | + panel.setPreferredSize(new Dimension(200,180)); |
| 91 | + |
| 92 | + |
| 93 | + popupMenu = new JPopupMenu(); |
| 94 | + menu = new JMenu("menu to hover"); |
| 95 | + menu.add(new JButton("JButton")); |
| 96 | + popupMenu.add(menu); |
| 97 | + |
| 98 | + panel.setComponentPopupMenu(popupMenu); |
| 99 | + frame.add(panel); |
| 100 | + frame.pack(); |
| 101 | + frame.setLocationRelativeTo(null); |
| 102 | + frame.setVisible(true); |
| 103 | + } |
| 104 | +} |
0 commit comments