bug4966112.java revision 4622
0N/A/*
3909N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A */
2362N/A
0N/A/*
0N/A * @test
0N/A * @bug 4966112
0N/A * @summary Some Composite components does not show the Context Popup.
0N/A * @library ../../regtesthelpers
0N/A * @build Util
0N/A * @author Alexander Zuev
0N/A * @run main bug4966112
0N/A */
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.event.PopupMenuListener;
0N/Aimport javax.swing.event.PopupMenuEvent;
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport sun.awt.SunToolkit;
0N/A
0N/Apublic class bug4966112 {
0N/A
0N/A private static final int NO_MOUSE_BUTTON = -1;
0N/A private static volatile boolean shown = false;
0N/A private static volatile int popupButton = NO_MOUSE_BUTTON;
0N/A private static volatile JButton testButton;
0N/A private static volatile JSplitPane jsp;
0N/A private static volatile JSpinner spin;
0N/A private static volatile JFileChooser filec;
0N/A private static int buttonMask;
0N/A private static Robot robot;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
0N/A robot = new Robot();
0N/A robot.setAutoDelay(100);
0N/A
3514N/A createAndShowButton();
0N/A toolkit.realSync();
3514N/A
10N/A setClickPoint(testButton);
10N/A clickMouse(InputEvent.BUTTON1_MASK);
3514N/A clickMouse(InputEvent.BUTTON2_MASK);
0N/A clickMouse(InputEvent.BUTTON3_MASK);
0N/A
0N/A toolkit.realSync();
0N/A closeFrame();
0N/A
0N/A if (popupButton == NO_MOUSE_BUTTON) {
0N/A System.out.println("Test can't identify the popup trigger button. Test skipped");
0N/A return;
0N/A }
0N/A
0N/A setButtonMask();
0N/A
0N/A // Test Split Pane
0N/A createAndShowSplitPane();
0N/A toolkit.realSync();
0N/A
0N/A clickMouse(jsp);
0N/A toolkit.realSync();
0N/A closeFrame();
0N/A
0N/A if (!shown) {
0N/A throw new RuntimeException("Popup was not shown on splitpane");
0N/A }
0N/A
0N/A // Test Spinner
0N/A createAndShowSpinner();
0N/A toolkit.realSync();
0N/A
0N/A clickMouse(spin);
0N/A toolkit.realSync();
0N/A closeFrame();
0N/A
0N/A if (!shown) {
0N/A throw new RuntimeException("Popup was not shown on spinner");
0N/A }
1220N/A
1220N/A // Test File Chooser
1220N/A createAndShowFileChooser();
1220N/A toolkit.realSync();
1220N/A
1220N/A clickMouse(filec);
1220N/A toolkit.realSync();
1220N/A
1220N/A Util.hitKeys(robot, KeyEvent.VK_ESCAPE);
1220N/A toolkit.realSync();
1220N/A
1220N/A Util.hitKeys(robot, KeyEvent.VK_ESCAPE);
1220N/A toolkit.realSync();
1220N/A closeFrame();
1220N/A
0N/A if (!shown) {
0N/A throw new RuntimeException("Popup was not shown on filechooser");
0N/A }
0N/A }
0N/A
0N/A private static void clickMouse(JComponent c) throws Exception {
0N/A setClickPoint(c);
0N/A clickMouse(buttonMask);
0N/A }
0N/A
0N/A private static void clickMouse(int buttons) {
0N/A robot.mousePress(buttons);
0N/A robot.mouseRelease(buttons);
0N/A }
0N/A
0N/A private static void setButtonMask() {
0N/A switch (popupButton) {
0N/A case MouseEvent.BUTTON1:
0N/A buttonMask = InputEvent.BUTTON1_MASK;
0N/A break;
0N/A case MouseEvent.BUTTON2:
0N/A buttonMask = InputEvent.BUTTON2_MASK;
914N/A break;
0N/A case MouseEvent.BUTTON3:
914N/A buttonMask = InputEvent.BUTTON3_MASK;
0N/A break;
0N/A }
2664N/A }
2664N/A
2664N/A private static void setClickPoint(final JComponent c) throws Exception {
2664N/A final Point[] result = new Point[1];
2664N/A SwingUtilities.invokeAndWait(new Runnable() {
0N/A
0N/A @Override
2664N/A public void run() {
0N/A Point p = c.getLocationOnScreen();
0N/A Dimension size = c.getSize();
2664N/A result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);
0N/A }
0N/A });
2664N/A
0N/A robot.mouseMove(result[0].x, result[0].y);
0N/A }
0N/A
0N/A private static JPopupMenu createJPopupMenu() {
0N/A JPopupMenu jpm = new JPopupMenu();
0N/A jpm.add("One");
0N/A jpm.add("Two");
914N/A jpm.add("Three");
914N/A jpm.addPopupMenuListener(new PopupMenuListener() {
914N/A
914N/A public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
914N/A shown = true;
914N/A }
914N/A
914N/A public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
914N/A }
914N/A
914N/A public void popupMenuCanceled(PopupMenuEvent e) {
914N/A }
914N/A });
914N/A
0N/A AutoClosable.INSTANCE.setPopup(jpm);
914N/A return jpm;
0N/A }
0N/A
0N/A private static void createAndShowButton() throws Exception {
0N/A SwingUtilities.invokeAndWait(new Runnable() {
0N/A
0N/A @Override
0N/A public void run() {
0N/A JFrame frame = new JFrame("Button Frame");
0N/A frame.setLayout(new BorderLayout());
0N/A testButton = new JButton("Popup Tester");
0N/A
0N/A testButton.addMouseListener(new MouseAdapter() {
0N/A
0N/A void setPopupTrigger(MouseEvent e) {
0N/A if (e.isPopupTrigger()) {
0N/A popupButton = e.getButton();
0N/A }
0N/A }
0N/A
0N/A public void mouseClicked(MouseEvent e) {
0N/A setPopupTrigger(e);
0N/A }
0N/A
0N/A public void mousePressed(MouseEvent e) {
0N/A setPopupTrigger(e);
0N/A }
0N/A
0N/A public void mouseReleased(MouseEvent e) {
0N/A setPopupTrigger(e);
0N/A }
0N/A });
0N/A
0N/A frame.add(testButton, BorderLayout.CENTER);
0N/A frame.pack();
0N/A frame.setVisible(true);
0N/A AutoClosable.INSTANCE.setFrame(frame);
0N/A }
0N/A });
0N/A }
420N/A
420N/A private static void createAndShowSplitPane() throws Exception {
420N/A SwingUtilities.invokeAndWait(new Runnable() {
420N/A
420N/A @Override
3689N/A public void run() {
3689N/A JFrame frame = new JFrame("Test SplitPane");
3689N/A frame.setSize(250, 200);
3689N/A frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
3689N/A frame.setLayout(new BorderLayout());
420N/A
2807N/A shown = false;
0N/A jsp = new JSplitPane();
0N/A jsp.setRightComponent(new JPanel());
0N/A jsp.setLeftComponent(new JPanel());
0N/A jsp.setComponentPopupMenu(createJPopupMenu());
0N/A
0N/A frame.add(jsp, BorderLayout.CENTER);
0N/A
0N/A jsp.setDividerLocation(150);
0N/A
0N/A frame.setLocation(400, 300);
0N/A frame.setVisible(true);
0N/A AutoClosable.INSTANCE.setFrame(frame);
0N/A }
0N/A });
0N/A }
0N/A
0N/A private static void createAndShowSpinner() throws Exception {
0N/A SwingUtilities.invokeAndWait(new Runnable() {
0N/A
0N/A @Override
0N/A public void run() {
0N/A JFrame frame = new JFrame("JSpinner Test");
0N/A frame.setLayout(new BorderLayout());
0N/A frame.setSize(200, 100);
0N/A shown = false;
0N/A spin = new JSpinner();
0N/A spin.setComponentPopupMenu(createJPopupMenu());
0N/A frame.add(spin, BorderLayout.CENTER);
0N/A frame.setVisible(true);
0N/A AutoClosable.INSTANCE.setFrame(frame);
0N/A }
0N/A });
0N/A }
0N/A
0N/A private static void createAndShowFileChooser() throws Exception {
0N/A SwingUtilities.invokeLater(new Runnable() {
0N/A
0N/A @Override
0N/A public void run() {
0N/A JFrame frame = new JFrame("FileChooser test dialog");
0N/A frame.setSize(100, 100);
0N/A
0N/A shown = false;
0N/A filec = new JFileChooser();
0N/A filec.setComponentPopupMenu(createJPopupMenu());
0N/A filec.showOpenDialog(frame);
0N/A
0N/A frame.setVisible(true);
0N/A AutoClosable.INSTANCE.setFrame(frame);
0N/A }
0N/A });
0N/A }
0N/A
0N/A private static void closeFrame() {
0N/A SwingUtilities.invokeLater(new Runnable() {
0N/A
0N/A @Override
0N/A public void run() {
0N/A AutoClosable.INSTANCE.close();
0N/A }
0N/A });
0N/A }
0N/A
0N/A private static class AutoClosable {
0N/A
0N/A static final AutoClosable INSTANCE = new AutoClosable();
0N/A private JFrame frame;
3148N/A private JPopupMenu popup;
3148N/A
3148N/A public void setFrame(JFrame frame) {
3148N/A this.frame = frame;
3148N/A }
3148N/A
0N/A public void setPopup(JPopupMenu popup) {
0N/A this.popup = popup;
0N/A }
0N/A
0N/A public void close() {
0N/A frame.dispose();
0N/A if (popup != null) {
1365N/A popup.setVisible(false);
0N/A }
0N/A }
0N/A }
0N/A}
0N/A