1 /*
   2  * Copyright (c) 2011, 2012, 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  * @key headful
  27  * @bug 4966112
  28  * @summary Some Composite components does not show the Context Popup.
  29  * @library ../../regtesthelpers
  30  * @build Util
  31  * @author Alexander Zuev
  32  * @run main bug4966112
  33  */
  34 import javax.swing.*;
  35 import javax.swing.event.PopupMenuListener;
  36 import javax.swing.event.PopupMenuEvent;
  37 import java.awt.*;
  38 import java.awt.event.*;
  39 import sun.awt.SunToolkit;
  40 
  41 public class bug4966112 {
  42 
  43     private static final int NO_MOUSE_BUTTON = -1;
  44     private static volatile boolean shown = false;
  45     private static volatile int popupButton = NO_MOUSE_BUTTON;
  46     private static volatile JButton testButton;
  47     private static volatile JSplitPane jsp;
  48     private static volatile JSpinner spin;
  49     private static volatile JFileChooser filec;
  50     private static int buttonMask;
  51     private static Robot robot;
  52 
  53     public static void main(String[] args) throws Exception {
  54         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  55         robot = new Robot();
  56         robot.setAutoDelay(100);
  57 
  58         createAndShowButton();
  59         toolkit.realSync();
  60 
  61         setClickPoint(testButton);
  62         clickMouse(InputEvent.BUTTON1_MASK);
  63         clickMouse(InputEvent.BUTTON2_MASK);
  64         clickMouse(InputEvent.BUTTON3_MASK);
  65 
  66         toolkit.realSync();
  67         closeFrame();
  68 
  69         if (popupButton == NO_MOUSE_BUTTON) {
  70             System.out.println("Test can't identify the popup trigger button. Test skipped");
  71             return;
  72         }
  73 
  74         setButtonMask();
  75 
  76         // Test Split Pane
  77         createAndShowSplitPane();
  78         toolkit.realSync();
  79 
  80         clickMouse(jsp);
  81         toolkit.realSync();
  82         closeFrame();
  83 
  84         if (!shown) {
  85             throw new RuntimeException("Popup was not shown on splitpane");
  86         }
  87 
  88         // Test Spinner
  89         createAndShowSpinner();
  90         toolkit.realSync();
  91 
  92         clickMouse(spin);
  93         toolkit.realSync();
  94         closeFrame();
  95 
  96         if (!shown) {
  97             throw new RuntimeException("Popup was not shown on spinner");
  98         }
  99 
 100         // Test File Chooser
 101         createAndShowFileChooser();
 102         toolkit.realSync();
 103 
 104         clickMouse(filec);
 105         toolkit.realSync();
 106 
 107         Util.hitKeys(robot, KeyEvent.VK_ESCAPE);
 108         toolkit.realSync();
 109 
 110         Util.hitKeys(robot, KeyEvent.VK_ESCAPE);
 111         toolkit.realSync();
 112         closeFrame();
 113 
 114         if (!shown) {
 115             throw new RuntimeException("Popup was not shown on filechooser");
 116         }
 117     }
 118 
 119     private static void clickMouse(JComponent c) throws Exception {
 120         setClickPoint(c);
 121         clickMouse(buttonMask);
 122     }
 123 
 124     private static void clickMouse(int buttons) {
 125         robot.mousePress(buttons);
 126         robot.mouseRelease(buttons);
 127     }
 128 
 129     private static void setButtonMask() {
 130         switch (popupButton) {
 131             case MouseEvent.BUTTON1:
 132                 buttonMask = InputEvent.BUTTON1_MASK;
 133                 break;
 134             case MouseEvent.BUTTON2:
 135                 buttonMask = InputEvent.BUTTON2_MASK;
 136                 break;
 137             case MouseEvent.BUTTON3:
 138                 buttonMask = InputEvent.BUTTON3_MASK;
 139                 break;
 140         }
 141     }
 142 
 143     private static void setClickPoint(final JComponent c) throws Exception {
 144         final Point[] result = new Point[1];
 145         SwingUtilities.invokeAndWait(new Runnable() {
 146 
 147             @Override
 148             public void run() {
 149                 Point p = c.getLocationOnScreen();
 150                 Dimension size = c.getSize();
 151                 result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);
 152             }
 153         });
 154 
 155         robot.mouseMove(result[0].x, result[0].y);
 156     }
 157 
 158     private static JPopupMenu createJPopupMenu() {
 159         JPopupMenu jpm = new JPopupMenu();
 160         jpm.add("One");
 161         jpm.add("Two");
 162         jpm.add("Three");
 163         jpm.addPopupMenuListener(new PopupMenuListener() {
 164 
 165             public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
 166                 shown = true;
 167             }
 168 
 169             public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
 170             }
 171 
 172             public void popupMenuCanceled(PopupMenuEvent e) {
 173             }
 174         });
 175 
 176         AutoClosable.INSTANCE.setPopup(jpm);
 177         return jpm;
 178     }
 179 
 180     private static void createAndShowButton() throws Exception {
 181         SwingUtilities.invokeAndWait(new Runnable() {
 182 
 183             @Override
 184             public void run() {
 185                 JFrame frame = new JFrame("Button Frame");
 186                 frame.setLayout(new BorderLayout());
 187                 testButton = new JButton("Popup Tester");
 188 
 189                 testButton.addMouseListener(new MouseAdapter() {
 190 
 191                     void setPopupTrigger(MouseEvent e) {
 192                         if (e.isPopupTrigger()) {
 193                             popupButton = e.getButton();
 194                         }
 195                     }
 196 
 197                     public void mouseClicked(MouseEvent e) {
 198                         setPopupTrigger(e);
 199                     }
 200 
 201                     public void mousePressed(MouseEvent e) {
 202                         setPopupTrigger(e);
 203                     }
 204 
 205                     public void mouseReleased(MouseEvent e) {
 206                         setPopupTrigger(e);
 207                     }
 208                 });
 209 
 210                 frame.add(testButton, BorderLayout.CENTER);
 211                 frame.pack();
 212                 frame.setVisible(true);
 213                 AutoClosable.INSTANCE.setFrame(frame);
 214             }
 215         });
 216     }
 217 
 218     private static void createAndShowSplitPane() throws Exception {
 219         SwingUtilities.invokeAndWait(new Runnable() {
 220 
 221             @Override
 222             public void run() {
 223                 JFrame frame = new JFrame("Test SplitPane");
 224                 frame.setSize(250, 200);
 225                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 226                 frame.setLayout(new BorderLayout());
 227 
 228                 shown = false;
 229                 jsp = new JSplitPane();
 230                 jsp.setRightComponent(new JPanel());
 231                 jsp.setLeftComponent(new JPanel());
 232                 jsp.setComponentPopupMenu(createJPopupMenu());
 233 
 234                 frame.add(jsp, BorderLayout.CENTER);
 235 
 236                 jsp.setDividerLocation(150);
 237 
 238                 frame.setLocation(400, 300);
 239                 frame.setVisible(true);
 240                 AutoClosable.INSTANCE.setFrame(frame);
 241             }
 242         });
 243     }
 244 
 245     private static void createAndShowSpinner() throws Exception {
 246         SwingUtilities.invokeAndWait(new Runnable() {
 247 
 248             @Override
 249             public void run() {
 250                 JFrame frame = new JFrame("JSpinner Test");
 251                 frame.setLayout(new BorderLayout());
 252                 frame.setSize(200, 100);
 253                 shown = false;
 254                 spin = new JSpinner();
 255                 spin.setComponentPopupMenu(createJPopupMenu());
 256                 frame.add(spin, BorderLayout.CENTER);
 257                 frame.setVisible(true);
 258                 AutoClosable.INSTANCE.setFrame(frame);
 259             }
 260         });
 261     }
 262 
 263     private static void createAndShowFileChooser() throws Exception {
 264         SwingUtilities.invokeLater(new Runnable() {
 265 
 266             @Override
 267             public void run() {
 268                 JFrame frame = new JFrame("FileChooser test dialog");
 269                 frame.setSize(100, 100);
 270 
 271                 shown = false;
 272                 filec = new JFileChooser();
 273                 filec.setComponentPopupMenu(createJPopupMenu());
 274                 filec.showOpenDialog(frame);
 275 
 276                 frame.setVisible(true);
 277                 AutoClosable.INSTANCE.setFrame(frame);
 278             }
 279         });
 280     }
 281 
 282     private static void closeFrame() {
 283         SwingUtilities.invokeLater(new Runnable() {
 284 
 285             @Override
 286             public void run() {
 287                 AutoClosable.INSTANCE.close();
 288             }
 289         });
 290     }
 291 
 292     private static class AutoClosable {
 293 
 294         static final AutoClosable INSTANCE = new AutoClosable();
 295         private JFrame frame;
 296         private JPopupMenu popup;
 297 
 298         public void setFrame(JFrame frame) {
 299             this.frame = frame;
 300         }
 301 
 302         public void setPopup(JPopupMenu popup) {
 303             this.popup = popup;
 304         }
 305 
 306         public void close() {
 307             frame.dispose();
 308             if (popup != null) {
 309                 popup.setVisible(false);
 310             }
 311         }
 312     }
 313 }