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