alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (bug6495920.java)

This example Java source code file (bug6495920.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

assertionerror, awt, edt, event, exception, field, gnome, gui, jpanel, jpopupmenu, mouse_grabber_key, object, point, reflection, robot, swing, throwable

The bug6495920.java Java example source code

/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 6495920
 * @summary Tests that if the JPopupMenu.setVisible method throws an exception,
            interaction with GNOME is not crippled
 * @author Sergey Malenkov
 * @library ../..
 */

import sun.awt.AppContext;

import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.lang.reflect.Field;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicPopupMenuUI;

public class bug6495920 implements Thread.UncaughtExceptionHandler {

    public static void main(String[] args) throws Throwable {
        SwingTest.start(bug6495920.class);
    }

    private static Robot robot;
    private final JPanel panel;

    public bug6495920(JFrame frame) {
        JPopupMenu menu = new JPopupMenu() {
            public void setVisible(boolean visible) {
                super.setVisible(visible);
                throw new AssertionError(visible ? "show popup" : "hide popup");
            }
        };
        for (int i = 0; i < 10; i++) {
            menu.add(new JMenuItem(String.valueOf(i)));
        }
        this.panel = new JPanel();
        this.panel.setComponentPopupMenu(menu);
        frame.add(this.panel);
    }

    public void firstShowPopup() throws Exception {
        Point point = this.panel.getLocation();
        SwingUtilities.convertPointToScreen(point, this.panel);

        robot = new Robot(); // initialize shared static field first time
        robot.mouseMove(point.x + 1, point.y + 1);
        robot.mousePress(InputEvent.BUTTON3_MASK);
        Thread.currentThread().setUncaughtExceptionHandler(this);
        robot.mouseRelease(InputEvent.BUTTON3_MASK); // causes first AssertionError on EDT
    }

    public void secondHidePopup() {
        Point point = this.panel.getLocation();
        SwingUtilities.convertPointToScreen(point, this.panel);

        robot.mouseMove(point.x - 1, point.y - 1);
        Thread.currentThread().setUncaughtExceptionHandler(this);
        robot.mousePress(InputEvent.BUTTON1_MASK); // causes second AssertionError on EDT
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
    }

    public void thirdValidate() throws Exception {
        Field key = BasicPopupMenuUI.class.getDeclaredField("MOUSE_GRABBER_KEY");
        key.setAccessible(true);

        Object grabber = AppContext.getAppContext().get(key.get(null));
        if (grabber == null) {
            throw new Exception("cannot find a mouse grabber in app's context");
        }

        Field field = grabber.getClass().getDeclaredField("grabbedWindow");
        field.setAccessible(true);

        Object window = field.get(grabber);
        if (window != null) {
            throw new Exception("interaction with GNOME is crippled");
        }
    }

    public void uncaughtException(Thread thread, Throwable throwable) {
        System.out.println(throwable);
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java bug6495920.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.