1 /*
2  * Copyright (c) 2010, 2015, 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 /*
25  * @test
26  * @key headful
27  * @bug 6495920
28  * @summary Tests that if the JPopupMenu.setVisible method throws an exception,
29             interaction with GNOME is not crippled
30  * @author Sergey Malenkov
31  * @library ../..
32  * @modules java.desktop/sun.awt
33  * @modules java.desktop/javax.swing.plaf.basic:open
34  */
35 
36 import sun.awt.AppContext;
37 
38 import java.awt.Point;
39 import java.awt.Robot;
40 import java.awt.event.InputEvent;
41 import java.lang.reflect.Field;
42 import javax.swing.JFrame;
43 import javax.swing.JMenuItem;
44 import javax.swing.JPanel;
45 import javax.swing.JPopupMenu;
46 import javax.swing.SwingUtilities;
47 import javax.swing.plaf.basic.BasicPopupMenuUI;
48 
49 public class bug6495920 implements Thread.UncaughtExceptionHandler {
50 
main(String[] args)51     public static void main(String[] args) throws Throwable {
52         SwingTest.start(bug6495920.class);
53     }
54 
55     private static Robot robot;
56     private final JPanel panel;
57 
bug6495920(JFrame frame)58     public bug6495920(JFrame frame) {
59         JPopupMenu menu = new JPopupMenu() {
60             public void setVisible(boolean visible) {
61                 super.setVisible(visible);
62                 throw new AssertionError(visible ? "show popup" : "hide popup");
63             }
64         };
65         for (int i = 0; i < 10; i++) {
66             menu.add(new JMenuItem(String.valueOf(i)));
67         }
68         this.panel = new JPanel();
69         this.panel.setComponentPopupMenu(menu);
70         frame.add(this.panel);
71     }
72 
firstShowPopup()73     public void firstShowPopup() throws Exception {
74         Point point = this.panel.getLocation();
75         SwingUtilities.convertPointToScreen(point, this.panel);
76 
77         robot = new Robot(); // initialize shared static field first time
78         robot.mouseMove(point.x + 1, point.y + 1);
79         robot.mousePress(InputEvent.BUTTON3_MASK);
80         Thread.currentThread().setUncaughtExceptionHandler(this);
81         robot.mouseRelease(InputEvent.BUTTON3_MASK); // causes first AssertionError on EDT
82     }
83 
secondHidePopup()84     public void secondHidePopup() {
85         Point point = this.panel.getLocation();
86         SwingUtilities.convertPointToScreen(point, this.panel);
87 
88         robot.mouseMove(point.x - 1, point.y - 1);
89         Thread.currentThread().setUncaughtExceptionHandler(this);
90         robot.mousePress(InputEvent.BUTTON1_MASK); // causes second AssertionError on EDT
91         robot.mouseRelease(InputEvent.BUTTON1_MASK);
92     }
93 
thirdValidate()94     public void thirdValidate() throws Exception {
95         Field key = BasicPopupMenuUI.class.getDeclaredField("MOUSE_GRABBER_KEY");
96         key.setAccessible(true);
97 
98         Object grabber = AppContext.getAppContext().get(key.get(null));
99         if (grabber == null) {
100             throw new Exception("cannot find a mouse grabber in app's context");
101         }
102 
103         Field field = grabber.getClass().getDeclaredField("grabbedWindow");
104         field.setAccessible(true);
105 
106         Object window = field.get(grabber);
107         if (window != null) {
108             throw new Exception("interaction with GNOME is crippled");
109         }
110     }
111 
uncaughtException(Thread thread, Throwable throwable)112     public void uncaughtException(Thread thread, Throwable throwable) {
113         System.out.println(throwable);
114     }
115 }
116