jdk/test/javax/swing/JPopupMenu/8147521/PopupMenuTest.java
changeset 39001 d446b5342c35
child 39027 60fc2577ef28
equal deleted inserted replaced
39000:810f4edc8b50 39001:d446b5342c35
       
     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
       
    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             frame.dispose();
       
    95         });
       
    96     }
       
    97 
       
    98     private void exectuteTest() {
       
    99         Point p = frame.getLocationOnScreen();
       
   100         Rectangle rect = frame.getBounds();
       
   101         robot.mouseMove(p.x + rect.width / 2, p.y + rect.height / 2);
       
   102         robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
       
   103         robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
       
   104         robot.delay(1000);
       
   105         robot.mouseMove(p.x + rect.width / 2 - 10, p.y + rect.height / 2 - 10);
       
   106         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
       
   107         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
       
   108         robot.delay(1000);
       
   109     }
       
   110 
       
   111     class MousePopupListener extends MouseAdapter {
       
   112 
       
   113         @Override
       
   114         public void mousePressed(MouseEvent e) {
       
   115             showPopup(e);
       
   116         }
       
   117 
       
   118         @Override
       
   119         public void mouseClicked(MouseEvent e) {
       
   120             showPopup(e);
       
   121         }
       
   122 
       
   123         @Override
       
   124         public void mouseReleased(MouseEvent e) {
       
   125             showPopup(e);
       
   126         }
       
   127 
       
   128         private void showPopup(MouseEvent e) {
       
   129             jpopup.show(panel, e.getX(), e.getY());
       
   130         }
       
   131     }
       
   132 
       
   133     class PopupListener implements PopupMenuListener {
       
   134 
       
   135         public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
       
   136         }
       
   137 
       
   138         public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
       
   139             Popup popup = ((PopMenuUIExt) jpopup.getUI()).getPopup();
       
   140             if (popup != null) {
       
   141                 isLightWeight = !popup.getClass().toString().
       
   142                         contains("HeavyWeightPopup");
       
   143             }
       
   144         }
       
   145 
       
   146         public void popupMenuCanceled(PopupMenuEvent e) {
       
   147         }
       
   148     }
       
   149 }
       
   150 
       
   151 class PopMenuUIExt extends BasicPopupMenuUI {
       
   152 
       
   153     private Popup popup;
       
   154 
       
   155     @Override
       
   156     public Popup getPopup(JPopupMenu popup, int x, int y) {
       
   157         PopupFactory.setSharedInstance(new PopupFactory() {
       
   158 
       
   159             @Override
       
   160             public Popup getPopup(Component owner, Component contents,
       
   161                     int x, int y) {
       
   162                 return super.getPopup(popup, popup x, y, true);
       
   163             }
       
   164         });
       
   165         PopupFactory factory = PopupFactory.getSharedInstance();
       
   166         popup = factory.getPopup(popup, popup, x, y);
       
   167         return popup;
       
   168     }
       
   169 
       
   170 
       
   171     public Popup getPopup() {
       
   172         return popup;
       
   173     }
       
   174 }
       
   175