Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JPopupMenu/4966112/bug4966112.java
38918 views
/*1* Copyright (c) 2011, 2012, 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 496611226* @summary Some Composite components does not show the Context Popup.27* @library ../../regtesthelpers28* @build Util29* @author Alexander Zuev30* @run main bug496611231*/32import javax.swing.*;33import javax.swing.event.PopupMenuListener;34import javax.swing.event.PopupMenuEvent;35import java.awt.*;36import java.awt.event.*;3738public class bug4966112 {3940private static final int NO_MOUSE_BUTTON = -1;41private static volatile boolean shown = false;42private static volatile int popupButton = NO_MOUSE_BUTTON;43private static volatile JButton testButton;44private static volatile JSplitPane jsp;45private static volatile JSpinner spin;46private static volatile JFileChooser filec;47private static int buttonMask;48private static Robot robot;4950public static void main(String[] args) throws Exception {51robot = new Robot();52robot.setAutoDelay(100);5354createAndShowButton();55robot.waitForIdle();5657setClickPoint(testButton);58clickMouse(InputEvent.BUTTON1_MASK);59clickMouse(InputEvent.BUTTON2_MASK);60clickMouse(InputEvent.BUTTON3_MASK);6162robot.waitForIdle();63closeFrame();6465if (popupButton == NO_MOUSE_BUTTON) {66System.out.println("Test can't identify the popup trigger button. Test skipped");67return;68}6970setButtonMask();7172// Test Split Pane73createAndShowSplitPane();74robot.waitForIdle();7576clickMouse(jsp);77robot.waitForIdle();78closeFrame();7980if (!shown) {81throw new RuntimeException("Popup was not shown on splitpane");82}8384// Test Spinner85createAndShowSpinner();86robot.waitForIdle();8788clickMouse(spin);89robot.waitForIdle();90closeFrame();9192if (!shown) {93throw new RuntimeException("Popup was not shown on spinner");94}9596// Test File Chooser97createAndShowFileChooser();98robot.waitForIdle();99100clickMouse(filec);101robot.waitForIdle();102103Util.hitKeys(robot, KeyEvent.VK_ESCAPE);104robot.waitForIdle();105106Util.hitKeys(robot, KeyEvent.VK_ESCAPE);107robot.waitForIdle();108closeFrame();109110if (!shown) {111throw new RuntimeException("Popup was not shown on filechooser");112}113}114115private static void clickMouse(JComponent c) throws Exception {116setClickPoint(c);117clickMouse(buttonMask);118}119120private static void clickMouse(int buttons) {121robot.mousePress(buttons);122robot.mouseRelease(buttons);123}124125private static void setButtonMask() {126switch (popupButton) {127case MouseEvent.BUTTON1:128buttonMask = InputEvent.BUTTON1_MASK;129break;130case MouseEvent.BUTTON2:131buttonMask = InputEvent.BUTTON2_MASK;132break;133case MouseEvent.BUTTON3:134buttonMask = InputEvent.BUTTON3_MASK;135break;136}137}138139private static void setClickPoint(final JComponent c) throws Exception {140final Point[] result = new Point[1];141SwingUtilities.invokeAndWait(new Runnable() {142143@Override144public void run() {145Point p = c.getLocationOnScreen();146Dimension size = c.getSize();147result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);148}149});150151robot.mouseMove(result[0].x, result[0].y);152}153154private static JPopupMenu createJPopupMenu() {155JPopupMenu jpm = new JPopupMenu();156jpm.add("One");157jpm.add("Two");158jpm.add("Three");159jpm.addPopupMenuListener(new PopupMenuListener() {160161public void popupMenuWillBecomeVisible(PopupMenuEvent e) {162shown = true;163}164165public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {166}167168public void popupMenuCanceled(PopupMenuEvent e) {169}170});171172AutoClosable.INSTANCE.setPopup(jpm);173return jpm;174}175176private static void createAndShowButton() throws Exception {177SwingUtilities.invokeAndWait(new Runnable() {178179@Override180public void run() {181JFrame frame = new JFrame("Button Frame");182frame.setLayout(new BorderLayout());183testButton = new JButton("Popup Tester");184185testButton.addMouseListener(new MouseAdapter() {186187void setPopupTrigger(MouseEvent e) {188if (e.isPopupTrigger()) {189popupButton = e.getButton();190}191}192193public void mouseClicked(MouseEvent e) {194setPopupTrigger(e);195}196197public void mousePressed(MouseEvent e) {198setPopupTrigger(e);199}200201public void mouseReleased(MouseEvent e) {202setPopupTrigger(e);203}204});205206frame.add(testButton, BorderLayout.CENTER);207frame.pack();208frame.setVisible(true);209AutoClosable.INSTANCE.setFrame(frame);210}211});212}213214private static void createAndShowSplitPane() throws Exception {215SwingUtilities.invokeAndWait(new Runnable() {216217@Override218public void run() {219JFrame frame = new JFrame("Test SplitPane");220frame.setSize(250, 200);221frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);222frame.setLayout(new BorderLayout());223224shown = false;225jsp = new JSplitPane();226jsp.setRightComponent(new JPanel());227jsp.setLeftComponent(new JPanel());228jsp.setComponentPopupMenu(createJPopupMenu());229230frame.add(jsp, BorderLayout.CENTER);231232jsp.setDividerLocation(150);233234frame.setLocation(400, 300);235frame.setVisible(true);236AutoClosable.INSTANCE.setFrame(frame);237}238});239}240241private static void createAndShowSpinner() throws Exception {242SwingUtilities.invokeAndWait(new Runnable() {243244@Override245public void run() {246JFrame frame = new JFrame("JSpinner Test");247frame.setLayout(new BorderLayout());248frame.setSize(200, 100);249shown = false;250spin = new JSpinner();251spin.setComponentPopupMenu(createJPopupMenu());252frame.add(spin, BorderLayout.CENTER);253frame.setVisible(true);254AutoClosable.INSTANCE.setFrame(frame);255}256});257}258259private static void createAndShowFileChooser() throws Exception {260SwingUtilities.invokeLater(new Runnable() {261262@Override263public void run() {264JFrame frame = new JFrame("FileChooser test dialog");265frame.setSize(100, 100);266267shown = false;268filec = new JFileChooser();269filec.setComponentPopupMenu(createJPopupMenu());270filec.showOpenDialog(frame);271272frame.setVisible(true);273AutoClosable.INSTANCE.setFrame(frame);274}275});276}277278private static void closeFrame() {279SwingUtilities.invokeLater(new Runnable() {280281@Override282public void run() {283AutoClosable.INSTANCE.close();284}285});286}287288private static class AutoClosable {289290static final AutoClosable INSTANCE = new AutoClosable();291private JFrame frame;292private JPopupMenu popup;293294public void setFrame(JFrame frame) {295this.frame = frame;296}297298public void setPopup(JPopupMenu popup) {299this.popup = popup;300}301302public void close() {303frame.dispose();304if (popup != null) {305popup.setVisible(false);306}307}308}309}310311312