Path: blob/master/test/jdk/javax/swing/JPopupMenu/8075063/ContextMenuScrollTest.java
41155 views
/*1* Copyright (c) 2017, 2021, 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 807506326* @summary Verifies if Context menu closes on mouse scroll27* @run main ContextMenuScrollTest28*/29import java.awt.Dimension;30import java.awt.Point;31import java.awt.Robot;32import java.awt.event.ActionEvent;33import java.awt.event.ActionListener;34import java.awt.event.InputEvent;35import java.awt.event.KeyEvent;36import javax.swing.JComponent;37import javax.swing.JFrame;38import javax.swing.JMenu;39import javax.swing.JMenuBar;40import javax.swing.JMenuItem;41import javax.swing.JPopupMenu;42import javax.swing.JSeparator;43import javax.swing.KeyStroke;44import javax.swing.SwingUtilities;4546public class ContextMenuScrollTest extends JPopupMenu47{48private static Robot robot;49private static JFrame frame;50private static JMenu menu;51private static volatile Point p = null;52private static volatile Dimension d = null;53private static volatile boolean popupVisible = false;5455public static void main(String[] args) throws Exception {56robot = new Robot();57robot.setAutoDelay(100);58try {59SwingUtilities.invokeAndWait(()->createGUI());60robot.waitForIdle();61robot.delay(1000);6263SwingUtilities.invokeAndWait(() -> {64p = menu.getLocationOnScreen();65d = menu.getSize();66});67System.out.println("p " + p + " d " + d);68robot.mouseMove(p.x + d.width/2, p.y + d.height/2);69robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);70robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);71robot.waitForIdle();72robot.delay(1000);7374robot.mouseWheel(1);75robot.waitForIdle();7677SwingUtilities.invokeAndWait(() -> {78popupVisible = menu.isPopupMenuVisible();79});80if (!popupVisible) {81throw new RuntimeException("Popup closes on mouse scroll");82}83} finally {84SwingUtilities.invokeAndWait(()->frame.dispose());85}86}878889public static void createGUI() {90frame = new JFrame();91JMenuBar menuBar = new JMenuBar();92menu = new JMenu("Menu");93menuBar.add(menu);9495JMenuItem undo = new JMenuItem("Undo");96undo.setEnabled(false);97undo.setAccelerator(KeyStroke.getKeyStroke("control Z"));98undo.addActionListener(new ActionListener() {99@Override100public void actionPerformed(ActionEvent event) {101}102});103104menu.add(undo);105106JMenuItem redo = new JMenuItem("Redo");107redo.setEnabled(false);108redo.setAccelerator(KeyStroke.getKeyStroke("control Y"));109redo.addActionListener(new ActionListener() {110@Override111public void actionPerformed(ActionEvent event) {112}113});114menu.add(redo);115116menu.add(new JSeparator());117118JMenuItem cut = new JMenuItem("Cut");119cut.setEnabled(false);120cut.setAccelerator(KeyStroke.getKeyStroke("control X"));121cut.addActionListener(new ActionListener() {122@Override123public void actionPerformed(ActionEvent event) {124}125});126127menu.add(cut);128129JMenuItem copy = new JMenuItem("Copy");130copy.setEnabled(false);131copy.setAccelerator(KeyStroke.getKeyStroke("control C"));132copy.addActionListener(new ActionListener() {133@Override134public void actionPerformed(ActionEvent event) {135}136});137138menu.add(copy);139140JMenuItem paste = new JMenuItem("Paste");141paste.setEnabled(false);142paste.setAccelerator(KeyStroke.getKeyStroke("control V"));143paste.addActionListener(new ActionListener() {144@Override145public void actionPerformed(ActionEvent event) {146}147});148149menu.add(paste);150151JMenuItem delete = new JMenuItem("Delete");152delete.setEnabled(false);153delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));154delete.addActionListener(new ActionListener() {155@Override156public void actionPerformed(ActionEvent event) {157}158});159160menu.add(delete);161162menu.add(new JSeparator());163164JMenuItem selectAll = new JMenuItem("Select All");165selectAll.setEnabled(false);166selectAll.setAccelerator(KeyStroke.getKeyStroke("control A"));167selectAll.addActionListener(new ActionListener() {168@Override169public void actionPerformed(ActionEvent event) {170}171});172menu.add(selectAll);173frame.setJMenuBar(menuBar);174175frame.pack();176frame.setLocationRelativeTo(null);177frame.setVisible(true);178}179}180181182