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