Path: blob/master/test/jdk/javax/swing/JPopupMenu/SetInvokerJPopupMenuTest.java
41149 views
/*1* Copyright (c) 2020, 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*/2223import javax.swing.JButton;24import javax.swing.JFrame;25import javax.swing.JMenuItem;26import javax.swing.JPopupMenu;27import javax.swing.SwingUtilities;28import javax.swing.UIManager;29import javax.swing.UnsupportedLookAndFeelException;30import java.awt.Container;31import java.awt.FlowLayout;32import java.awt.Point;33import java.awt.Robot;34import java.awt.event.ActionEvent;35import java.awt.event.ActionListener;36import java.awt.event.InputEvent;37import java.awt.event.MouseEvent;38import java.io.StringWriter;39import java.io.PrintWriter;4041/**42* @test43* @bug 804810944* @summary JToggleButton does not fire actionPerformed under certain45* conditions46* @key headful47* @run main SetInvokerJPopupMenuTest48*/4950public class SetInvokerJPopupMenuTest {5152private static MyPopupMenu popup;53private static MyButton jtb ;54private static Robot robot;55private static JFrame f;56private static Point p;57private static boolean isPopupVisible;5859public static void main(String[] args) throws Exception {60UIManager.LookAndFeelInfo[] installedLookAndFeels;61installedLookAndFeels = UIManager.getInstalledLookAndFeels();6263for(UIManager.LookAndFeelInfo LF : installedLookAndFeels) {64try {65robot = new Robot();66UIManager.setLookAndFeel(LF.getClassName());67SwingUtilities.invokeAndWait(() -> {68jtb = new MyButton("Press Me");69jtb.addActionListener(new ActionListener( ) {70@Override71public void actionPerformed(ActionEvent ev) {72if (!popup.isVisible()) {73postUp();74}75else {76postDown();77}78}79});8081f = new JFrame( );82f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);83f.setLocationRelativeTo(null);84f.setSize(300, 400);85Container c = f.getContentPane( );86c.setLayout(new FlowLayout( ));87c.add(jtb);88f.setVisible(true);89popup = new MyPopupMenu();90popup.add(new JMenuItem("A"));91popup.add(new JMenuItem("B"));92popup.add(new JMenuItem("C"));93popup.setVisible(false);94p = jtb.getLocationOnScreen();95});9697robot.waitForIdle();98robot.setAutoDelay(50);99robot.mouseMove(p.x + 15, p.y + 15);100robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);101robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);102103robot.waitForIdle();104robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);105robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);106107} catch (UnsupportedLookAndFeelException e) {108System.out.println("Note: LookAndFeel " + LF.getClassName()109+ " is not supported on this configuration");110} finally {111if (f != null) {112SwingUtilities.invokeAndWait(() -> f.dispose());113}114}115116SwingUtilities.invokeAndWait(() -> {117isPopupVisible = popup.isVisible();118});119120if (isPopupVisible) {121throw new RuntimeException("PopupMenu is not taken down after"+122" single button click");123}124}125}126127public static void postUp() {128popup.setInvoker(jtb);129popup.setVisible(true);130}131132public static void postDown() {133popup.setVisible(false);134}135136private static class MyButton extends JButton {137public MyButton(String string) {138super (string);139}140@Override141protected void processMouseEvent(MouseEvent e) {142super.processMouseEvent(e);143}144}145146private static class MyPopupMenu extends JPopupMenu {147@Override148public void setVisible( boolean state ) {149if( !state ) {150Exception ex = new Exception();151StringWriter stringWriter = new StringWriter();152PrintWriter printWriter = new PrintWriter( stringWriter );153ex.printStackTrace( printWriter );154String traceString = stringWriter.getBuffer().toString();155if( traceString.lastIndexOf( "windowDeactivated" ) > 0156|| traceString.lastIndexOf( "menuSelectionChanged" )157> 0 ) {158return;159}160}161super.setVisible(state);162}163}164}165166167