2534N/A/*
2534N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2534N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2534N/A *
2534N/A * This code is free software; you can redistribute it and/or modify it
2534N/A * under the terms of the GNU General Public License version 2 only, as
2534N/A * published by the Free Software Foundation.
2534N/A *
2534N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2534N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2534N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2534N/A * version 2 for more details (a copy is included in the LICENSE file that
2534N/A * accompanied this code).
2534N/A *
2534N/A * You should have received a copy of the GNU General Public License version
2534N/A * 2 along with this work; if not, write to the Free Software Foundation,
2534N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2534N/A *
2534N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2534N/A * or visit www.oracle.com if you need additional information or have any
2534N/A * questions.
2534N/A */
2534N/A
2534N/A/*
2534N/A * @test
2534N/A * @bug 6495920
2534N/A * @summary Tests that if the JPopupMenu.setVisible method throws an exception,
2534N/A interaction with GNOME is not crippled
2534N/A * @author Sergey Malenkov
2534N/A * @library ../..
2534N/A */
2534N/A
2534N/Aimport sun.awt.AppContext;
2534N/A
2534N/Aimport java.awt.Point;
2534N/Aimport java.awt.Robot;
2534N/Aimport java.awt.event.InputEvent;
2534N/Aimport java.lang.reflect.Field;
2534N/Aimport javax.swing.JFrame;
2534N/Aimport javax.swing.JMenuItem;
2534N/Aimport javax.swing.JPanel;
2534N/Aimport javax.swing.JPopupMenu;
2534N/Aimport javax.swing.SwingUtilities;
2534N/Aimport javax.swing.plaf.basic.BasicPopupMenuUI;
2534N/A
2534N/Apublic class bug6495920 implements Thread.UncaughtExceptionHandler {
2534N/A
2534N/A public static void main(String[] args) throws Throwable {
2534N/A SwingTest.start(bug6495920.class);
2534N/A }
2534N/A
2534N/A private static Robot robot;
2534N/A private final JPanel panel;
2534N/A
2534N/A public bug6495920(JFrame frame) {
2534N/A JPopupMenu menu = new JPopupMenu() {
2534N/A public void setVisible(boolean visible) {
2534N/A super.setVisible(visible);
2534N/A throw new AssertionError(visible ? "show popup" : "hide popup");
2534N/A }
2534N/A };
2534N/A for (int i = 0; i < 10; i++) {
2534N/A menu.add(new JMenuItem(String.valueOf(i)));
2534N/A }
2534N/A this.panel = new JPanel();
2534N/A this.panel.setComponentPopupMenu(menu);
2534N/A frame.add(this.panel);
2534N/A }
2534N/A
2534N/A public void firstShowPopup() throws Exception {
2534N/A Point point = this.panel.getLocation();
2534N/A SwingUtilities.convertPointToScreen(point, this.panel);
2534N/A
2534N/A robot = new Robot(); // initialize shared static field first time
2534N/A robot.mouseMove(point.x + 1, point.y + 1);
2534N/A robot.mousePress(InputEvent.BUTTON3_MASK);
2534N/A Thread.currentThread().setUncaughtExceptionHandler(this);
2534N/A robot.mouseRelease(InputEvent.BUTTON3_MASK); // causes first AssertionError on EDT
2534N/A }
2534N/A
2534N/A public void secondHidePopup() {
2534N/A Point point = this.panel.getLocation();
2534N/A SwingUtilities.convertPointToScreen(point, this.panel);
2534N/A
2534N/A robot.mouseMove(point.x - 1, point.y - 1);
2534N/A Thread.currentThread().setUncaughtExceptionHandler(this);
2534N/A robot.mousePress(InputEvent.BUTTON1_MASK); // causes second AssertionError on EDT
2534N/A robot.mouseRelease(InputEvent.BUTTON1_MASK);
2534N/A }
2534N/A
2534N/A public void thirdValidate() throws Exception {
2534N/A Field key = BasicPopupMenuUI.class.getDeclaredField("MOUSE_GRABBER_KEY");
2534N/A key.setAccessible(true);
2534N/A
2534N/A Object grabber = AppContext.getAppContext().get(key.get(null));
2534N/A if (grabber == null) {
2534N/A throw new Exception("cannot find a mouse grabber in app's context");
2534N/A }
2534N/A
2534N/A Field field = grabber.getClass().getDeclaredField("grabbedWindow");
2534N/A field.setAccessible(true);
2534N/A
2534N/A Object window = field.get(grabber);
2534N/A if (window != null) {
2534N/A throw new Exception("interaction with GNOME is crippled");
2534N/A }
2534N/A }
2534N/A
2534N/A public void uncaughtException(Thread thread, Throwable throwable) {
2534N/A System.out.println(throwable);
2534N/A }
2534N/A}