Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JPopupMenu/6415145/bug6415145.java
38918 views
/*1* Copyright (c) 2006, 2014, 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@bug 641514525@summary REGRESSION: Selected item is not being updated while dragging above popup menu26@library ../../../../lib/testlibrary27@build ExtendedRobot28@author Mikhail Lapshin29@run main bug641514530*/3132import javax.swing.*;33import java.awt.event.*;34import java.awt.AWTException;35import java.awt.Component;3637public class bug6415145 {38private JFrame frame;39private JButton button;40private JPopupMenu popupMenu;41private JMenuItem item1;42private JMenuItem item2;43private static ExtendedRobot robot;4445public static void main(String[] args) throws Exception {46robot = new ExtendedRobot();47final bug6415145 bugTest = new bug6415145();48try {49SwingUtilities.invokeAndWait(new Runnable() {50public void run() {51bugTest.init();52}53});5455robot.waitForIdle();56bugTest.test();57} finally {58bugTest.stopEDT();59}60}6162private void stopEDT() {63if (frame != null) {64frame.dispose();65}66}6768private void init() {69popupMenu = new JPopupMenu("test menu");70item1 = new JMenuItem("item 1");71item2 = new JMenuItem("item 2");72popupMenu.add(item1);73popupMenu.add(item2);7475button = new JButton("test button");76button.addMouseListener(new MouseListener());7778frame = new JFrame("test frame");79frame.add(popupMenu);80frame.add(button);81frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);82frame.setSize(200, 200);83frame.setLocationRelativeTo(null);84frame.setVisible(true);85}8687private class MouseListener extends MouseAdapter {88public void mousePressed(MouseEvent e) {89popupMenu.show(button, e.getX(), e.getY());90}91}9293private void test() throws AWTException {94try {95moveMouseTo(robot, button);96robot.mousePress(InputEvent.BUTTON1_MASK);97robot.waitForIdle();9899moveMouseTo(robot, item1);100robot.waitForIdle();101102moveMouseTo(robot, item2);103robot.waitForIdle();104if ( (item1.isArmed()) || (!item2.isArmed()) ) {105throw new RuntimeException("Selected item is not being updated" +106" while dragging above popup menu.");107}108} finally {109robot.mouseRelease(InputEvent.BUTTON1_MASK);110}111}112private void moveMouseTo(ExtendedRobot robot, Component c) {113java.awt.Point p = c.getLocationOnScreen();114java.awt.Dimension size = c.getSize();115p.x += size.width / 2;116p.y += size.height / 2;117robot.mouseMove(p.x, p.y);118robot.delay(100);119}120}121122123