1/*
2 * Copyright (c) 2017, 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 * @test
25 * @key headful
26 * @bug 4870644 7190539
27 * @summary  Default button responds to CTRL-ENTER while popup menu is active.
28 * @run main bug4870644
29 */
30import java.awt.BorderLayout;
31import java.awt.Dimension;
32import java.awt.Point;
33import java.awt.Robot;
34import java.awt.event.KeyEvent;
35import java.awt.event.ActionEvent;
36import java.awt.event.InputEvent;
37import javax.swing.AbstractAction;
38import javax.swing.JButton;
39import javax.swing.JComponent;
40import javax.swing.JFrame;
41import javax.swing.JMenu;
42import javax.swing.JMenuBar;
43import javax.swing.JMenuItem;
44import javax.swing.JPopupMenu;
45import javax.swing.SwingUtilities;
46import javax.swing.UIManager;
47import javax.swing.UnsupportedLookAndFeelException;
48
49
50public class bug4870644 {
51    JButton b1, b2, b3;
52    JFrame frame;
53    JMenu menu;
54    JPopupMenu popup;
55    static Robot robot;
56    static boolean passed = true;
57    private volatile Point p = null;
58    private volatile Dimension d = null;
59    void blockTillDisplayed(JComponent comp) throws Exception {
60        while (p == null) {
61            try {
62                SwingUtilities.invokeAndWait(() -> {
63                    p = comp.getLocationOnScreen();
64                    d = comp.getSize();
65                });
66            } catch (IllegalStateException e) {
67                try {
68                    Thread.sleep(1000);
69                } catch (InterruptedException ie) {
70                }
71            }
72        }
73    }
74
75    private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {
76        try {
77            UIManager.setLookAndFeel(laf.getClassName());
78            System.out.println("LookAndFeel: " + laf.getClassName());
79        } catch (ClassNotFoundException | InstantiationException |
80                UnsupportedLookAndFeelException | IllegalAccessException e) {
81            throw new RuntimeException(e);
82        }
83    }
84
85    public static void main(String[] args) throws Exception {
86        robot = new Robot();
87        robot.setAutoDelay(100);
88        for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
89            try {
90                SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
91                System.out.println("Test for LookAndFeel " + laf.getClassName());
92                new bug4870644();
93                System.out.println("Test passed for LookAndFeel " + laf.getClassName());
94            } catch (Exception e) {
95                throw new RuntimeException(e);
96            }
97        }
98    }
99
100    public bug4870644() throws Exception {
101
102        SwingUtilities.invokeAndWait(() -> {
103            JMenuBar menuBar = new JMenuBar();
104            menu = new JMenu("Menu");
105            menuBar.add(menu);
106            JMenuItem menuItem = new JMenuItem("Item");
107            menu.add(menuItem);
108            menu.add(new JMenuItem("Item 2"));
109            frame = new JFrame("test");
110            frame.setJMenuBar(menuBar);
111
112            b1 = new JButton("One");
113            b2 = new JButton("Two");
114            b3 = new JButton("Default");
115            b3.addActionListener(new AbstractAction() {
116                public void actionPerformed(ActionEvent e) {
117                    passed = false;
118                }
119            });
120            frame.getContentPane().add(b1, BorderLayout.NORTH);
121            frame.getContentPane().add(b2, BorderLayout.CENTER);
122            frame.getContentPane().add(b3, BorderLayout.SOUTH);
123            frame.getRootPane().setDefaultButton(b3);
124            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
125            frame.pack();
126            frame.setVisible(true);
127        });
128
129        blockTillDisplayed(b1);
130        robot.waitForIdle();
131
132        robot.delay(500);
133        robot.mouseMove(p.x + d.width-1, p.y + d.height/2);
134        robot.mousePress(InputEvent.BUTTON1_MASK);
135        robot.mouseRelease(InputEvent.BUTTON1_MASK);
136        robot.waitForIdle();
137        robot.keyPress(KeyEvent.VK_F10);
138        robot.keyRelease(KeyEvent.VK_F10);
139        robot.keyPress(KeyEvent.VK_DOWN);
140        robot.keyRelease(KeyEvent.VK_DOWN);
141        robot.waitForIdle();
142
143        SwingUtilities.invokeAndWait(() -> {
144            popup = menu.getPopupMenu();
145        });
146
147        blockTillDisplayed(popup);
148        robot.waitForIdle();
149        robot.mouseMove(p.x + d.width-1, p.y + d.height/2);
150        robot.keyPress(KeyEvent.VK_CONTROL);
151        robot.keyPress(KeyEvent.VK_ENTER);
152        robot.keyRelease(KeyEvent.VK_ENTER);
153        robot.keyRelease(KeyEvent.VK_CONTROL);
154        robot.waitForIdle();
155
156        SwingUtilities.invokeAndWait(() -> frame.dispose());
157        if(!passed) {
158            String cause = "Default button reacted on \"ctrl ENTER\" while menu is active.";
159            throw new RuntimeException(cause);
160        }
161    }
162}
163