Path: blob/master/test/jdk/javax/swing/JPopupMenu/4966112/bug4966112.java
41153 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* @key headful26* @bug 496611227* @summary Some Composite components does not show the Context Popup.28* @library ../../regtesthelpers29* @build Util30* @author Alexander Zuev31* @run main bug496611232*/33import javax.swing.JButton;34import javax.swing.JComponent;35import javax.swing.JFileChooser;36import javax.swing.JFrame;37import javax.swing.JPanel;38import javax.swing.JPopupMenu;39import javax.swing.JSpinner;40import javax.swing.JSplitPane;41import javax.swing.SwingUtilities;42import javax.swing.UIManager;43import javax.swing.event.PopupMenuListener;44import javax.swing.event.PopupMenuEvent;4546import java.awt.BorderLayout;47import java.awt.Dimension;48import java.awt.Robot;49import java.awt.Point;50import java.awt.event.InputEvent;51import java.awt.event.KeyEvent;52import java.awt.event.MouseAdapter;53import java.awt.event.MouseEvent;5455public class bug4966112 {5657private static final int NO_MOUSE_BUTTON = -1;58private static volatile boolean shown = false;59private static volatile int popupButton = NO_MOUSE_BUTTON;60private static volatile JButton testButton;61private static volatile JSplitPane jsp;62private static volatile JSpinner spin;63private static volatile JFileChooser filec;64private static int buttonMask;65private static Robot robot;66private static boolean isAquaFileChooser;6768public static void main(String[] args) throws Exception {69robot = new Robot();70robot.setAutoDelay(100);7172createAndShowButton();73robot.waitForIdle();7475setClickPoint(testButton);76clickMouse(InputEvent.BUTTON1_MASK);77clickMouse(InputEvent.BUTTON2_MASK);78clickMouse(InputEvent.BUTTON3_MASK);7980robot.waitForIdle();81closeFrame();8283if (popupButton == NO_MOUSE_BUTTON) {84System.out.println("Test can't identify the popup trigger button. Test skipped");85return;86}8788setButtonMask();8990// Test Split Pane91createAndShowSplitPane();92robot.waitForIdle();9394clickMouse(jsp);95robot.waitForIdle();96closeFrame();9798if (!shown) {99throw new RuntimeException("Popup was not shown on splitpane");100}101102// Test Spinner103createAndShowSpinner();104robot.waitForIdle();105106clickMouse(spin);107robot.waitForIdle();108closeFrame();109110if (!shown) {111throw new RuntimeException("Popup was not shown on spinner");112}113114// Test File Chooser115createAndShowFileChooser();116robot.waitForIdle();117118if ((UIManager.getLookAndFeel().getID()).equals("Aqua")) {119isAquaFileChooser = true;120} else {121isAquaFileChooser = false;122}123clickMouse(filec);124robot.waitForIdle();125126Util.hitKeys(robot, KeyEvent.VK_ESCAPE);127robot.waitForIdle();128129Util.hitKeys(robot, KeyEvent.VK_ESCAPE);130robot.waitForIdle();131closeFrame();132133if (!shown) {134throw new RuntimeException("Popup was not shown on filechooser");135}136}137138private static void clickMouse(JComponent c) throws Exception {139setClickPoint(c);140clickMouse(buttonMask);141}142143private static void clickMouse(int buttons) {144robot.mousePress(buttons);145robot.mouseRelease(buttons);146}147148private static void setButtonMask() {149switch (popupButton) {150case MouseEvent.BUTTON1:151buttonMask = InputEvent.BUTTON1_MASK;152break;153case MouseEvent.BUTTON2:154buttonMask = InputEvent.BUTTON2_MASK;155break;156case MouseEvent.BUTTON3:157buttonMask = InputEvent.BUTTON3_MASK;158break;159}160}161162private static void setClickPoint(final JComponent c) throws Exception {163final Point[] result = new Point[1];164SwingUtilities.invokeAndWait(new Runnable() {165166@Override167public void run() {168Point p = c.getLocationOnScreen();169Dimension size = c.getSize();170if (isAquaFileChooser) {171result[0] = new Point(p.x + size.width / 2, p.y + 5);172} else {173result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);174}175}176});177178robot.mouseMove(result[0].x, result[0].y);179}180181private static JPopupMenu createJPopupMenu() {182JPopupMenu jpm = new JPopupMenu();183jpm.add("One");184jpm.add("Two");185jpm.add("Three");186jpm.addPopupMenuListener(new PopupMenuListener() {187188public void popupMenuWillBecomeVisible(PopupMenuEvent e) {189shown = true;190}191192public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {193}194195public void popupMenuCanceled(PopupMenuEvent e) {196}197});198199AutoClosable.INSTANCE.setPopup(jpm);200return jpm;201}202203private static void createAndShowButton() throws Exception {204SwingUtilities.invokeAndWait(new Runnable() {205206@Override207public void run() {208JFrame frame = new JFrame("Button Frame");209frame.setLayout(new BorderLayout());210testButton = new JButton("Popup Tester");211212testButton.addMouseListener(new MouseAdapter() {213214void setPopupTrigger(MouseEvent e) {215if (e.isPopupTrigger()) {216popupButton = e.getButton();217}218}219220public void mouseClicked(MouseEvent e) {221setPopupTrigger(e);222}223224public void mousePressed(MouseEvent e) {225setPopupTrigger(e);226}227228public void mouseReleased(MouseEvent e) {229setPopupTrigger(e);230}231});232233frame.add(testButton, BorderLayout.CENTER);234frame.pack();235frame.setVisible(true);236AutoClosable.INSTANCE.setFrame(frame);237}238});239}240241private static void createAndShowSplitPane() throws Exception {242SwingUtilities.invokeAndWait(new Runnable() {243244@Override245public void run() {246JFrame frame = new JFrame("Test SplitPane");247frame.setSize(250, 200);248frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);249frame.setLayout(new BorderLayout());250251shown = false;252jsp = new JSplitPane();253jsp.setRightComponent(new JPanel());254jsp.setLeftComponent(new JPanel());255jsp.setComponentPopupMenu(createJPopupMenu());256257frame.add(jsp, BorderLayout.CENTER);258259jsp.setDividerLocation(150);260261frame.setLocation(400, 300);262frame.setVisible(true);263AutoClosable.INSTANCE.setFrame(frame);264}265});266}267268private static void createAndShowSpinner() throws Exception {269SwingUtilities.invokeAndWait(new Runnable() {270271@Override272public void run() {273JFrame frame = new JFrame("JSpinner Test");274frame.setLayout(new BorderLayout());275frame.setSize(200, 100);276shown = false;277spin = new JSpinner();278spin.setComponentPopupMenu(createJPopupMenu());279frame.add(spin, BorderLayout.CENTER);280frame.setVisible(true);281AutoClosable.INSTANCE.setFrame(frame);282}283});284}285286private static void createAndShowFileChooser() throws Exception {287SwingUtilities.invokeLater(new Runnable() {288289@Override290public void run() {291JFrame frame = new JFrame("FileChooser test dialog");292frame.setSize(100, 100);293294shown = false;295filec = new JFileChooser();296filec.setComponentPopupMenu(createJPopupMenu());297filec.showOpenDialog(frame);298299frame.setVisible(true);300AutoClosable.INSTANCE.setFrame(frame);301}302});303}304305private static void closeFrame() {306SwingUtilities.invokeLater(new Runnable() {307308@Override309public void run() {310AutoClosable.INSTANCE.close();311}312});313}314315private static class AutoClosable {316317static final AutoClosable INSTANCE = new AutoClosable();318private JFrame frame;319private JPopupMenu popup;320321public void setFrame(JFrame frame) {322this.frame = frame;323}324325public void setPopup(JPopupMenu popup) {326this.popup = popup;327}328329public void close() {330frame.dispose();331if (popup != null) {332popup.setVisible(false);333}334}335}336}337338339