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