1 /*
   2  * Copyright (c) 2003, 2018, 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 4634626
  28  * @summary Implement context popup menus for components
  29  * @library ../../../../lib/testlibrary
  30  * @build ExtendedRobot
  31  * @run main bug4634626
  32  */
  33 
  34 import javax.swing.*;
  35 import java.awt.event.*;
  36 import java.awt.*;
  37 
  38 public class bug4634626 {
  39 
  40     public boolean passed = true;
  41     public boolean done = false;
  42 
  43     public JFrame mainFrame = new JFrame("Bug4634626");
  44     public JRootPane rootPane = mainFrame.getRootPane();
  45     public JPanel contentPane = new JPanel();
  46     public JButton nopButton = new JButton("No popup button");
  47     public JTextArea someText = new JTextArea("Some text here", 20, 10);
  48     public JButton popButton = new JButton("Button with the popup");
  49 
  50     public JPopupMenu btnPopup = new JPopupMenu();
  51     public JPopupMenu commonPopup = new JPopupMenu();
  52     static public Error toBeThrown = null;
  53     static int popTrig = MouseEvent.BUTTON3_MASK;
  54     static boolean popt = false;
  55 
  56     public static class MouseWatcher extends MouseAdapter {
  57         public void mousePressed(MouseEvent e) {
  58             if(e.isPopupTrigger()) popt = true;
  59             if(e.getComponent() != null &&
  60                e.getComponent() instanceof JComponent &&
  61                e.isPopupTrigger() &&
  62                ((JComponent)e.getComponent()).getComponentPopupMenu() != null) {
  63                 toBeThrown =
  64                   new Error("The event got thru the component with popup: "
  65                   + e);
  66             }
  67         }
  68         public void mouseReleased(MouseEvent e) {
  69             if(e.isPopupTrigger()) popt = true;
  70             if(e.getComponent() != null &&
  71                e.getComponent() instanceof JComponent &&
  72                e.isPopupTrigger() &&
  73                ((JComponent)e.getComponent()).getComponentPopupMenu() != null) {
  74                 toBeThrown =
  75                   new Error("The event got thru the component with popup: "
  76                   + e);
  77             }
  78             if(toBeThrown != null) {
  79                 throw(toBeThrown);
  80             }
  81         }
  82     }
  83 
  84     public static MouseWatcher mouser = new MouseWatcher();
  85 
  86     public static void main(final String[] args) {
  87         bug4634626 app = new bug4634626();
  88         app.init();
  89         app.destroy();
  90     }
  91 
  92     public void init() {
  93 
  94         try {
  95             popButton.setComponentPopupMenu(null);
  96             popButton.setComponentPopupMenu(null);
  97             popButton.setComponentPopupMenu(btnPopup);
  98             popButton.setComponentPopupMenu(null);
  99         } catch(Exception ex) {
 100             System.err.println("Unexpected exception was thrown by " +
 101                                "setComponentPopupMenu() method: " + ex);
 102         }
 103         btnPopup.add("Button 1");
 104         btnPopup.add("Button 2");
 105         btnPopup.add("Button 3");
 106         popButton.setComponentPopupMenu(btnPopup);
 107         popButton.addMouseListener(mouser);
 108         commonPopup.add("One");
 109         commonPopup.add("Two");
 110         commonPopup.add("Three");
 111 
 112         contentPane.setLayout(new BorderLayout());
 113         contentPane.setComponentPopupMenu(commonPopup);
 114         contentPane.addMouseListener(mouser);
 115         contentPane.add(nopButton, BorderLayout.NORTH);
 116         nopButton.addMouseListener(mouser);
 117         contentPane.add(popButton, BorderLayout.SOUTH);
 118         someText.addMouseListener(mouser);
 119         contentPane.add(someText, BorderLayout.CENTER);
 120         mainFrame.setContentPane(contentPane);
 121 
 122         mainFrame.pack();
 123         mainFrame.setLocation(50, 50);
 124 
 125         mainFrame.addWindowListener(new TestStateListener());
 126         mainFrame.setLocationRelativeTo(null);
 127         mainFrame.setVisible(true);
 128 
 129         while(!done) Thread.yield();
 130 
 131         if(!passed) {
 132             throw new RuntimeException("Test failed");
 133         }
 134 
 135     }
 136 
 137     public class TestStateListener extends WindowAdapter {
 138         public void windowOpened(WindowEvent ev) {
 139             try {
 140                 ev.getWindow().toFront();
 141                 ev.getWindow().requestFocus();
 142                 new Thread(new RobotThread()).start();
 143             } catch (Exception ex) {
 144                 throw new RuntimeException("Thread Exception");
 145             }
 146         }
 147     }
 148 
 149     class RobotThread implements Runnable {
 150         public void run() {
 151             ExtendedRobot robo;
 152             try {
 153                 robo = new ExtendedRobot();
 154             }catch(Exception ex) {
 155                 ex.printStackTrace();
 156                 throw new RuntimeException("Cannot create Robot");
 157             }
 158             robo.setAutoDelay(100);
 159             robo.waitForIdle();
 160 
 161             // Determine working popup trigger event
 162             clickMouseOn(robo, nopButton, popTrig);
 163             robo.waitForIdle();
 164             robo.delay(500);
 165             if(!popt) popTrig = MouseEvent.BUTTON2_MASK;
 166 
 167             // Inheritance is OFF by default. Popup should not appear.
 168             clickMouseOn(robo, someText, popTrig);
 169 
 170             // Set inheritance ON watch for popup.
 171             someText.setInheritsPopupMenu(true);
 172             clickMouseOn(robo, someText, popTrig);
 173             robo.waitForIdle();
 174             robo.delay(500);
 175             if(!commonPopup.isVisible()) {
 176                 toBeThrown = new Error("Popup should be visible");
 177                 passed = false;
 178             }
 179             // Dispose popup.
 180             robo.type(KeyEvent.VK_ESCAPE);
 181             robo.waitForIdle();
 182             someText.setInheritsPopupMenu(false);
 183 
 184             // Button with popup assigned. Wathch for popup.
 185             clickMouseOn(robo, popButton, popTrig);
 186             robo.waitForIdle();
 187             robo.delay(500);
 188             if(!btnPopup.isVisible()) {
 189                 toBeThrown = new Error("Popup should be visible");
 190                 passed = false;
 191             }
 192             // Dispose popup.
 193             robo.type(KeyEvent.VK_ESCAPE);
 194             // Test finished.
 195             done = true;
 196         }
 197     }
 198 
 199 
 200 
 201     public void destroy() {
 202         if(!passed) {
 203             throw(toBeThrown);
 204         }
 205     }
 206     private void clickMouseOn(ExtendedRobot robot, Component c, int button) {
 207         java.awt.Point p = c.getLocationOnScreen();
 208         java.awt.Dimension size = c.getSize();
 209         p.x += size.width / 2;
 210         p.y += size.height / 2;
 211         robot.mouseMove(p.x, p.y);
 212         robot.delay(100);
 213         robot.click(button);
 214     }
 215 }