Path: blob/master/test/jdk/javax/swing/JPopupMenu/6495920/bug6495920.java
41153 views
/*1* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 649592027* @summary Tests that if the JPopupMenu.setVisible method throws an exception,28interaction with GNOME is not crippled29* @author Sergey Malenkov30* @library ../..31* @modules java.desktop/sun.awt32* @modules java.desktop/javax.swing.plaf.basic:open33*/3435import sun.awt.AppContext;3637import 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;4748public class bug6495920 implements Thread.UncaughtExceptionHandler {4950public static void main(String[] args) throws Throwable {51SwingTest.start(bug6495920.class);52}5354private static Robot robot;55private final JPanel panel;5657public bug6495920(JFrame frame) {58JPopupMenu menu = new JPopupMenu() {59public void setVisible(boolean visible) {60super.setVisible(visible);61throw new AssertionError(visible ? "show popup" : "hide popup");62}63};64for (int i = 0; i < 10; i++) {65menu.add(new JMenuItem(String.valueOf(i)));66}67this.panel = new JPanel();68this.panel.setComponentPopupMenu(menu);69frame.add(this.panel);70}7172public void firstShowPopup() throws Exception {73Point point = this.panel.getLocation();74SwingUtilities.convertPointToScreen(point, this.panel);7576robot = new Robot(); // initialize shared static field first time77robot.mouseMove(point.x + 1, point.y + 1);78robot.mousePress(InputEvent.BUTTON3_MASK);79Thread.currentThread().setUncaughtExceptionHandler(this);80robot.mouseRelease(InputEvent.BUTTON3_MASK); // causes first AssertionError on EDT81}8283public void secondHidePopup() {84Point point = this.panel.getLocation();85SwingUtilities.convertPointToScreen(point, this.panel);8687robot.mouseMove(point.x - 1, point.y - 1);88Thread.currentThread().setUncaughtExceptionHandler(this);89robot.mousePress(InputEvent.BUTTON1_MASK); // causes second AssertionError on EDT90robot.mouseRelease(InputEvent.BUTTON1_MASK);91}9293public void thirdValidate() throws Exception {94Field key = BasicPopupMenuUI.class.getDeclaredField("MOUSE_GRABBER_KEY");95key.setAccessible(true);9697Object grabber = AppContext.getAppContext().get(key.get(null));98if (grabber == null) {99throw new Exception("cannot find a mouse grabber in app's context");100}101102Field field = grabber.getClass().getDeclaredField("grabbedWindow");103field.setAccessible(true);104105Object window = field.get(grabber);106if (window != null) {107throw new Exception("interaction with GNOME is crippled");108}109}110111public void uncaughtException(Thread thread, Throwable throwable) {112System.out.println(throwable);113}114}115116117