Path: blob/master/test/jdk/javax/swing/JPopupMenu/6415145/bug6415145.java
41153 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* @key headful25* @bug 641514526* @summary REGRESSION: Selected item is not being updated while dragging above popup menu27* @library /lib/client28* @build ExtendedRobot29* @author Mikhail Lapshin30* @run main bug641514531*/3233import javax.swing.*;34import java.awt.event.*;35import java.awt.AWTException;36import java.awt.Component;3738public class bug6415145 {39private JFrame frame;40private JButton button;41private JPopupMenu popupMenu;42private JMenuItem item1;43private JMenuItem item2;44private static ExtendedRobot robot;4546public static void main(String[] args) throws Exception {47robot = new ExtendedRobot();48final bug6415145 bugTest = new bug6415145();49try {50SwingUtilities.invokeAndWait(new Runnable() {51public void run() {52bugTest.init();53}54});5556robot.waitForIdle();57bugTest.test();58} finally {59bugTest.stopEDT();60}61}6263private void stopEDT() {64if (frame != null) {65frame.dispose();66}67}6869private void init() {70popupMenu = new JPopupMenu("test menu");71item1 = new JMenuItem("item 1");72item2 = new JMenuItem("item 2");73popupMenu.add(item1);74popupMenu.add(item2);7576button = new JButton("test button");77button.addMouseListener(new MouseListener());7879frame = new JFrame("test frame");80frame.add(popupMenu);81frame.add(button);82frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);83frame.setSize(200, 200);84frame.setLocationRelativeTo(null);85frame.setVisible(true);86}8788private class MouseListener extends MouseAdapter {89public void mousePressed(MouseEvent e) {90popupMenu.show(button, e.getX(), e.getY());91}92}9394private void test() throws AWTException {95try {96moveMouseTo(robot, button);97robot.mousePress(InputEvent.BUTTON1_MASK);98robot.waitForIdle();99100moveMouseTo(robot, item1);101robot.waitForIdle();102103moveMouseTo(robot, item2);104robot.waitForIdle();105if ( (item1.isArmed()) || (!item2.isArmed()) ) {106throw new RuntimeException("Selected item is not being updated" +107" while dragging above popup menu.");108}109} finally {110robot.mouseRelease(InputEvent.BUTTON1_MASK);111}112}113private void moveMouseTo(ExtendedRobot robot, Component c) {114java.awt.Point p = c.getLocationOnScreen();115java.awt.Dimension size = c.getSize();116p.x += size.width / 2;117p.y += size.height / 2;118robot.mouseMove(p.x, p.y);119robot.delay(100);120}121}122123124