Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JPopupMenu/6495920/bug6495920.java
38854 views
/*1* Copyright (c) 2010, 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* @bug 649592026* @summary Tests that if the JPopupMenu.setVisible method throws an exception,27interaction with GNOME is not crippled28* @author Sergey Malenkov29* @library ../..30*/3132import sun.awt.AppContext;3334import java.awt.Point;35import java.awt.Robot;36import java.awt.event.InputEvent;37import java.lang.reflect.Field;38import javax.swing.JFrame;39import javax.swing.JMenuItem;40import javax.swing.JPanel;41import javax.swing.JPopupMenu;42import javax.swing.SwingUtilities;43import javax.swing.plaf.basic.BasicPopupMenuUI;4445public class bug6495920 implements Thread.UncaughtExceptionHandler {4647public static void main(String[] args) throws Throwable {48SwingTest.start(bug6495920.class);49}5051private static Robot robot;52private final JPanel panel;5354public bug6495920(JFrame frame) {55JPopupMenu menu = new JPopupMenu() {56public void setVisible(boolean visible) {57super.setVisible(visible);58throw new AssertionError(visible ? "show popup" : "hide popup");59}60};61for (int i = 0; i < 10; i++) {62menu.add(new JMenuItem(String.valueOf(i)));63}64this.panel = new JPanel();65this.panel.setComponentPopupMenu(menu);66frame.add(this.panel);67}6869public void firstShowPopup() throws Exception {70Point point = this.panel.getLocation();71SwingUtilities.convertPointToScreen(point, this.panel);7273robot = new Robot(); // initialize shared static field first time74robot.mouseMove(point.x + 1, point.y + 1);75robot.mousePress(InputEvent.BUTTON3_MASK);76Thread.currentThread().setUncaughtExceptionHandler(this);77robot.mouseRelease(InputEvent.BUTTON3_MASK); // causes first AssertionError on EDT78}7980public void secondHidePopup() {81Point point = this.panel.getLocation();82SwingUtilities.convertPointToScreen(point, this.panel);8384robot.mouseMove(point.x - 1, point.y - 1);85Thread.currentThread().setUncaughtExceptionHandler(this);86robot.mousePress(InputEvent.BUTTON1_MASK); // causes second AssertionError on EDT87robot.mouseRelease(InputEvent.BUTTON1_MASK);88}8990public void thirdValidate() throws Exception {91Field key = BasicPopupMenuUI.class.getDeclaredField("MOUSE_GRABBER_KEY");92key.setAccessible(true);9394Object grabber = AppContext.getAppContext().get(key.get(null));95if (grabber == null) {96throw new Exception("cannot find a mouse grabber in app's context");97}9899Field field = grabber.getClass().getDeclaredField("grabbedWindow");100field.setAccessible(true);101102Object window = field.get(grabber);103if (window != null) {104throw new Exception("interaction with GNOME is crippled");105}106}107108public void uncaughtException(Thread thread, Throwable throwable) {109System.out.println(throwable);110}111}112113114