1 /*
   2  * Copyright (c) 2006, 2014, 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 @test
  25 @bug 6415145
  26 @summary REGRESSION: Selected item is not being updated while dragging above popup menu
  27 @library ../../../../lib/testlibrary
  28 @build ExtendedRobot
  29 @author Mikhail Lapshin
  30 @run main bug6415145
  31 */
  32 
  33 import javax.swing.*;
  34 import java.awt.event.*;
  35 import java.awt.AWTException;
  36 import java.awt.Component;
  37 
  38 public class bug6415145 {
  39     private JFrame frame;
  40     private JButton button;
  41     private JPopupMenu popupMenu;
  42     private JMenuItem item1;
  43     private JMenuItem item2;
  44     private static ExtendedRobot robot;
  45 
  46     public static void main(String[] args) throws Exception {
  47         robot = new ExtendedRobot();
  48         final bug6415145 bugTest = new bug6415145();
  49         try {
  50             SwingUtilities.invokeAndWait(new Runnable() {
  51                 public void run() {
  52                     bugTest.init();
  53                 }
  54             });
  55 
  56             robot.waitForIdle();
  57             bugTest.test();
  58         } finally {
  59             bugTest.stopEDT();
  60         }
  61     }
  62 
  63     private void stopEDT() {
  64         if (frame != null) {
  65             frame.dispose();
  66         }
  67     }
  68 
  69     private void init() {
  70         popupMenu = new JPopupMenu("test menu");
  71         item1 = new JMenuItem("item 1");
  72         item2 = new JMenuItem("item 2");
  73         popupMenu.add(item1);
  74         popupMenu.add(item2);
  75 
  76         button = new JButton("test button");
  77         button.addMouseListener(new MouseListener());
  78 
  79         frame = new JFrame("test frame");
  80         frame.add(popupMenu);
  81         frame.add(button);
  82         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  83         frame.setSize(200, 200);
  84         frame.setLocationRelativeTo(null);
  85         frame.setVisible(true);
  86     }
  87 
  88     private class MouseListener extends MouseAdapter {
  89         public void mousePressed(MouseEvent e) {
  90             popupMenu.show(button, e.getX(), e.getY());
  91         }
  92     }
  93 
  94     private void test() throws AWTException {
  95         try {
  96             moveMouseTo(robot, button);
  97             robot.mousePress(InputEvent.BUTTON1_MASK);
  98             robot.waitForIdle();
  99 
 100             moveMouseTo(robot, item1);
 101             robot.waitForIdle();
 102 
 103             moveMouseTo(robot, item2);
 104             robot.waitForIdle();
 105             if ( (item1.isArmed()) || (!item2.isArmed()) ) {
 106                 throw new RuntimeException("Selected item is not being updated" +
 107                         " while dragging above popup menu.");
 108             }
 109         } finally {
 110             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 111         }
 112     }
 113     private void moveMouseTo(ExtendedRobot robot, Component c) {
 114         java.awt.Point p = c.getLocationOnScreen();
 115         java.awt.Dimension size = c.getSize();
 116         p.x += size.width / 2;
 117         p.y += size.height / 2;
 118         robot.mouseMove(p.x, p.y);
 119         robot.delay(100);
 120     }
 121 }