1 /*
   2  * Copyright (c) 2016, 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  * @bug 8147521 8158358
  27  * @summary [macosx] Internal API Usage: setPopupType used to force creation of
  28  * heavyweight popup
  29  * @run main PopupMenuTest
  30  */
  31 import java.awt.Component;
  32 import java.awt.Point;
  33 import java.awt.Rectangle;
  34 import java.awt.Robot;
  35 import java.awt.event.InputEvent;
  36 import java.awt.event.MouseAdapter;
  37 import java.awt.event.MouseEvent;
  38 import javax.swing.JFrame;
  39 import javax.swing.JMenuItem;
  40 import javax.swing.JPanel;
  41 import javax.swing.JPopupMenu;
  42 import javax.swing.Popup;
  43 import javax.swing.PopupFactory;
  44 import javax.swing.SwingUtilities;
  45 import javax.swing.event.PopupMenuEvent;
  46 import javax.swing.event.PopupMenuListener;
  47 import javax.swing.plaf.basic.BasicPopupMenuUI;
  48 
  49 public class PopupMenuTest {
  50 
  51     private JPopupMenu jpopup;
  52     private static volatile boolean isLightWeight;
  53     private static JFrame frame;
  54     private static Robot robot;
  55     private static JPanel panel;
  56 
  57     public static void main(String s[]) throws Exception {
  58         PopupMenuTest obj = new PopupMenuTest();
  59         obj.createUI();
  60         robot = new Robot();
  61         robot.waitForIdle();
  62         robot.delay(1000);
  63         obj.exectuteTest();
  64         obj.dispose();
  65         if (isLightWeight) {
  66             throw new RuntimeException("Test Failed");
  67         }
  68     }
  69 
  70     private void createUI() throws Exception {
  71         SwingUtilities.invokeAndWait(() -> {
  72             frame = new JFrame("Popup Menu");
  73             jpopup = new JPopupMenu();
  74             jpopup.setUI(new PopMenuUIExt());
  75             JMenuItem item = new JMenuItem("Menu Item1");
  76             jpopup.add(item);
  77             item = new JMenuItem("Menu Item2");
  78             jpopup.setLabel("Justification");
  79             jpopup.add(item);
  80             jpopup.setLabel("Justification");
  81             jpopup.addPopupMenuListener(new PopupListener());
  82             panel = new JPanel();
  83             panel.addMouseListener(new MousePopupListener());
  84             frame.setContentPane(panel);
  85             frame.setSize(300, 300);
  86             frame.setLocationRelativeTo(null);
  87             frame.setVisible(true);
  88         });
  89 
  90     }
  91 
  92     private void dispose() throws Exception {
  93         SwingUtilities.invokeAndWait(() -> {
  94             Popup popup = PopMenuUIExt.getPopup();
  95             if (popup != null) {
  96                 popup.hide();
  97             }
  98             frame.dispose();
  99         });
 100     }
 101 
 102     private void exectuteTest() {
 103         Point p = frame.getLocationOnScreen();
 104         Rectangle rect = frame.getBounds();
 105         robot.mouseMove(p.x + rect.width / 2, p.y + rect.height / 2);
 106         robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
 107         robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
 108         robot.delay(1000);
 109         robot.mouseMove(p.x + rect.width / 2 - 10, p.y + rect.height / 2 - 10);
 110         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 111         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 112         robot.delay(1000);
 113     }
 114 
 115     class MousePopupListener extends MouseAdapter {
 116 
 117         @Override
 118         public void mousePressed(MouseEvent e) {
 119             showPopup(e);
 120         }
 121 
 122         @Override
 123         public void mouseClicked(MouseEvent e) {
 124             showPopup(e);
 125         }
 126 
 127         @Override
 128         public void mouseReleased(MouseEvent e) {
 129             showPopup(e);
 130         }
 131 
 132         private void showPopup(MouseEvent e) {
 133             jpopup.show(panel, e.getX(), e.getY());
 134         }
 135     }
 136 
 137     class PopupListener implements PopupMenuListener {
 138 
 139         public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
 140         }
 141 
 142         public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
 143             Popup popup = ((PopMenuUIExt) jpopup.getUI()).getPopup();
 144             if (popup != null) {
 145                 isLightWeight = !popup.getClass().toString().
 146                         contains("HeavyWeightPopup");
 147             }
 148         }
 149 
 150         public void popupMenuCanceled(PopupMenuEvent e) {
 151         }
 152     }
 153 }
 154 
 155 class PopMenuUIExt extends BasicPopupMenuUI {
 156 
 157     private static Popup popUp;
 158 
 159     @Override
 160     public Popup getPopup(JPopupMenu popup, int x, int y) {
 161         PopupFactory.setSharedInstance(new PopupFactory() {
 162 
 163             @Override
 164             public Popup getPopup(Component owner, Component contents,
 165                     int x, int y) {
 166                 return super.getPopup(owner, contents, x, y, true);
 167             }
 168         });
 169         PopupFactory factory = PopupFactory.getSharedInstance();
 170         popUp = factory.getPopup(popup.getInvoker(), popup, x, y);
 171         return popUp;
 172     }
 173 
 174     public static Popup getPopup() {
 175         return popUp;
 176     }
 177 }
 178