1 /*
2  * Copyright (c) 2017, 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 8075063
27  * @summary  Verifies if Context menu closes on mouse scroll
28  * @run main ContextMenuScrollTest
29  */
30 import java.awt.Dimension;
31 import java.awt.Point;
32 import java.awt.Robot;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.awt.event.InputEvent;
36 import java.awt.event.KeyEvent;
37 import javax.swing.JComponent;
38 import javax.swing.JFrame;
39 import javax.swing.JMenu;
40 import javax.swing.JMenuBar;
41 import javax.swing.JMenuItem;
42 import javax.swing.JPopupMenu;
43 import javax.swing.JSeparator;
44 import javax.swing.KeyStroke;
45 import javax.swing.SwingUtilities;
46 
47 public class ContextMenuScrollTest extends JPopupMenu
48 {
49     private JMenuItem undo;
50     private JMenuItem redo;
51     private JMenuItem cut;
52     private JMenuItem copy;
53     private JMenuItem paste;
54     private JMenuItem delete;
55     private JMenuItem selectAll;
56     private final Robot robot;
57     private JFrame frame;
58     private JMenuBar menuBar;
59     private JMenu menu;
60     private volatile Point p = null;
61     private volatile Dimension d = null;
62 
main(String[] args)63     public static void main(String[] args) throws Exception {
64         new ContextMenuScrollTest();
65     }
blockTillDisplayed(JComponent comp)66     void blockTillDisplayed(JComponent comp) throws Exception {
67         while (p == null) {
68             try {
69                 SwingUtilities.invokeAndWait(() -> {
70                     p = comp.getLocationOnScreen();
71                     d = menu.getSize();
72                 });
73             } catch (IllegalStateException e) {
74                 try {
75                     Thread.sleep(1000);
76                 } catch (InterruptedException ie) {
77                 }
78             }
79         }
80     }
81 
ContextMenuScrollTest()82     public ContextMenuScrollTest() throws Exception
83     {
84         robot = new Robot();
85         robot.setAutoDelay(200);
86         try {
87             SwingUtilities.invokeAndWait(()->createGUI());
88             blockTillDisplayed(menu);
89             robot.waitForIdle();
90 
91             robot.mouseMove(p.x + d.width/2, p.y + d.height/2);
92             robot.mousePress(InputEvent.BUTTON1_MASK);
93             robot.mouseRelease(InputEvent.BUTTON1_MASK);
94             robot.waitForIdle();
95 
96             System.out.println("popmenu visible " + menu.isPopupMenuVisible());
97             robot.mouseWheel(1);
98             robot.waitForIdle();
99             System.out.println("popmenu visible " + menu.isPopupMenuVisible());
100             if (!menu.isPopupMenuVisible()) {
101                 throw new RuntimeException("Popup closes on mouse scroll");
102             }
103         } finally {
104             SwingUtilities.invokeAndWait(()->frame.dispose());
105         }
106     }
107 
createGUI()108     public void createGUI() {
109         frame = new JFrame();
110         menuBar = new JMenuBar();
111         menu = new JMenu("Menu");
112         menuBar.add(menu);
113 
114         undo = new JMenuItem("Undo");
115         undo.setEnabled(false);
116         undo.setAccelerator(KeyStroke.getKeyStroke("control Z"));
117         undo.addActionListener(new ActionListener() {
118             @Override
119             public void actionPerformed(ActionEvent event) {
120             }
121         });
122 
123         menu.add(undo);
124 
125         redo = new JMenuItem("Redo");
126         redo.setEnabled(false);
127         redo.setAccelerator(KeyStroke.getKeyStroke("control Y"));
128         redo.addActionListener(new ActionListener() {
129             @Override
130             public void actionPerformed(ActionEvent event) {
131             }
132         });
133         menu.add(redo);
134 
135         menu.add(new JSeparator());
136 
137         cut = new JMenuItem("Cut");
138         cut.setEnabled(false);
139         cut.setAccelerator(KeyStroke.getKeyStroke("control X"));
140         cut.addActionListener(new ActionListener() {
141             @Override
142             public void actionPerformed(ActionEvent event) {
143             }
144         });
145 
146         menu.add(cut);
147 
148         copy = new JMenuItem("Copy");
149         copy.setEnabled(false);
150         copy.setAccelerator(KeyStroke.getKeyStroke("control C"));
151         copy.addActionListener(new ActionListener() {
152             @Override
153             public void actionPerformed(ActionEvent event) {
154             }
155         });
156 
157         menu.add(copy);
158 
159         paste = new JMenuItem("Paste");
160         paste.setEnabled(false);
161         paste.setAccelerator(KeyStroke.getKeyStroke("control V"));
162         paste.addActionListener(new ActionListener() {
163             @Override
164             public void actionPerformed(ActionEvent event) {
165             }
166         });
167 
168         menu.add(paste);
169 
170         delete = new JMenuItem("Delete");
171         delete.setEnabled(false);
172         delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
173         delete.addActionListener(new ActionListener() {
174             @Override
175             public void actionPerformed(ActionEvent event) {
176             }
177         });
178 
179         menu.add(delete);
180 
181         menu.add(new JSeparator());
182 
183         selectAll = new JMenuItem("Select All");
184         selectAll.setEnabled(false);
185         selectAll.setAccelerator(KeyStroke.getKeyStroke("control A"));
186         selectAll.addActionListener(new ActionListener() {
187             @Override
188             public void actionPerformed(ActionEvent event) {
189             }
190         });
191         menu.add(selectAll);
192         frame.setJMenuBar(menuBar);
193 
194         frame.pack();
195         frame.setVisible(true);
196     }
197 }
198