Path: blob/master/test/jdk/javax/swing/JPopupMenu/8147521/PopupMenuTest.java
41153 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 8147521 815835827* @summary [macosx] Internal API Usage: setPopupType used to force creation of28* heavyweight popup29* @run main PopupMenuTest30*/31import java.awt.Component;32import java.awt.Point;33import java.awt.Rectangle;34import java.awt.Robot;35import java.awt.event.InputEvent;36import java.awt.event.MouseAdapter;37import java.awt.event.MouseEvent;38import javax.swing.JFrame;39import javax.swing.JMenuItem;40import javax.swing.JPanel;41import javax.swing.JPopupMenu;42import javax.swing.Popup;43import javax.swing.PopupFactory;44import javax.swing.SwingUtilities;45import javax.swing.event.PopupMenuEvent;46import javax.swing.event.PopupMenuListener;47import javax.swing.plaf.basic.BasicPopupMenuUI;4849public class PopupMenuTest {5051private JPopupMenu jpopup;52private static volatile boolean isLightWeight;53private static JFrame frame;54private static Robot robot;55private static JPanel panel;5657public static void main(String s[]) throws Exception {58PopupMenuTest obj = new PopupMenuTest();59obj.createUI();60robot = new Robot();61robot.waitForIdle();62robot.delay(1000);63obj.exectuteTest();64obj.dispose();65if (isLightWeight) {66throw new RuntimeException("Test Failed");67}68}6970private void createUI() throws Exception {71SwingUtilities.invokeAndWait(() -> {72frame = new JFrame("Popup Menu");73jpopup = new JPopupMenu();74jpopup.setUI(new PopMenuUIExt());75JMenuItem item = new JMenuItem("Menu Item1");76jpopup.add(item);77item = new JMenuItem("Menu Item2");78jpopup.setLabel("Justification");79jpopup.add(item);80jpopup.setLabel("Justification");81jpopup.addPopupMenuListener(new PopupListener());82panel = new JPanel();83panel.addMouseListener(new MousePopupListener());84frame.setContentPane(panel);85frame.setSize(300, 300);86frame.setLocationRelativeTo(null);87frame.setVisible(true);88});8990}9192private void dispose() throws Exception {93SwingUtilities.invokeAndWait(() -> {94Popup popup = PopMenuUIExt.getPopup();95if (popup != null) {96popup.hide();97}98frame.dispose();99});100}101102private void exectuteTest() {103Point p = frame.getLocationOnScreen();104Rectangle rect = frame.getBounds();105robot.mouseMove(p.x + rect.width / 2, p.y + rect.height / 2);106robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);107robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);108robot.delay(1000);109robot.mouseMove(p.x + rect.width / 2 - 10, p.y + rect.height / 2 - 10);110robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);111robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);112robot.delay(1000);113}114115class MousePopupListener extends MouseAdapter {116117@Override118public void mousePressed(MouseEvent e) {119showPopup(e);120}121122@Override123public void mouseClicked(MouseEvent e) {124showPopup(e);125}126127@Override128public void mouseReleased(MouseEvent e) {129showPopup(e);130}131132private void showPopup(MouseEvent e) {133jpopup.show(panel, e.getX(), e.getY());134}135}136137class PopupListener implements PopupMenuListener {138139public void popupMenuWillBecomeVisible(PopupMenuEvent e) {140}141142public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {143Popup popup = ((PopMenuUIExt) jpopup.getUI()).getPopup();144if (popup != null) {145isLightWeight = !popup.getClass().toString().146contains("HeavyWeightPopup");147}148}149150public void popupMenuCanceled(PopupMenuEvent e) {151}152}153}154155class PopMenuUIExt extends BasicPopupMenuUI {156157private static Popup popUp;158159@Override160public Popup getPopup(JPopupMenu popup, int x, int y) {161PopupFactory.setSharedInstance(new PopupFactory() {162163@Override164public Popup getPopup(Component owner, Component contents,165int x, int y) {166return super.getPopup(owner, contents, x, y, true);167}168});169PopupFactory factory = PopupFactory.getSharedInstance();170popUp = factory.getPopup(popup.getInvoker(), popup, x, y);171return popUp;172}173174public static Popup getPopup() {175return popUp;176}177}178179180181