|
| 1 | +/* |
| 2 | + * Copyright (c) 2001, 2023, 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 | + @bug 4390019 |
| 26 | + @summary REGRESSION: Alt-F4 keybinding no longer shuts down java application on Windows |
| 27 | + @key headful |
| 28 | + @requires (os.family == "windows") |
| 29 | + @run main NoFocusOwnerAWTTest |
| 30 | +*/ |
| 31 | +import java.awt.Frame; |
| 32 | +import java.awt.BorderLayout; |
| 33 | +import java.awt.EventQueue; |
| 34 | +import java.awt.Label; |
| 35 | +import java.awt.MenuBar; |
| 36 | +import java.awt.Menu; |
| 37 | +import java.awt.MenuItem; |
| 38 | +import java.awt.MenuShortcut; |
| 39 | +import java.awt.Robot; |
| 40 | +import java.awt.event.ActionEvent; |
| 41 | +import java.awt.event.ActionListener; |
| 42 | +import java.awt.event.FocusEvent; |
| 43 | +import java.awt.event.FocusListener; |
| 44 | +import java.awt.event.KeyEvent; |
| 45 | +import java.awt.event.WindowEvent; |
| 46 | +import java.awt.event.WindowAdapter; |
| 47 | + |
| 48 | +public class NoFocusOwnerAWTTest { |
| 49 | + |
| 50 | + static boolean actionFired = false; |
| 51 | + static boolean closingWindowCalled = false; |
| 52 | + static Frame frame; |
| 53 | + |
| 54 | + public static void main(String[] args) throws Exception { |
| 55 | + try { |
| 56 | + if (!System.getProperty("os.name").startsWith("Windows")) { |
| 57 | + // this test is Win32 test only |
| 58 | + return; |
| 59 | + } |
| 60 | + EventQueue.invokeAndWait(() -> { |
| 61 | + |
| 62 | + frame = new Frame("No Focus Owner AWT Test"); |
| 63 | + frame.addWindowListener(new WindowAdapter() { |
| 64 | + public void windowClosing(WindowEvent e) { |
| 65 | + System.out.println("windowClosing() is called."); |
| 66 | + closingWindowCalled = true; |
| 67 | + } |
| 68 | + }); |
| 69 | + frame.addFocusListener(new FocusListener() { |
| 70 | + public void focusGained(FocusEvent fe) { |
| 71 | + System.out.println("focus gained on frame"); |
| 72 | + } |
| 73 | + public void focusLost(FocusEvent fe) { |
| 74 | + System.out.println("focus lost on frame"); |
| 75 | + } |
| 76 | + }); |
| 77 | + MenuBar mb = new MenuBar(); |
| 78 | + Menu m = new Menu("This is Menu"); |
| 79 | + MenuItem mi = new MenuItem("Menu Item"); |
| 80 | + mi.setShortcut(new MenuShortcut(KeyEvent.VK_A)); |
| 81 | + mi.addActionListener( new ActionListener() { |
| 82 | + public void actionPerformed(ActionEvent ae) { |
| 83 | + System.out.println("action"); |
| 84 | + actionFired = true; |
| 85 | + } |
| 86 | + }); |
| 87 | + m.add(mi); |
| 88 | + mb.add(m); |
| 89 | + frame.setMenuBar(mb); |
| 90 | + Label lb; |
| 91 | + frame.add(lb = new Label("press")); |
| 92 | + lb.addFocusListener(new FocusListener() { |
| 93 | + public void focusGained(FocusEvent fe) { |
| 94 | + System.out.println("focus gained on label"); |
| 95 | + } |
| 96 | + public void focusLost(FocusEvent fe) { |
| 97 | + System.out.println("focus lost on label"); |
| 98 | + } |
| 99 | + }); |
| 100 | + frame.pack(); |
| 101 | + frame.toFront(); |
| 102 | + frame.setLocationRelativeTo(null); |
| 103 | + frame.setVisible(true); |
| 104 | + }); |
| 105 | + |
| 106 | + Robot robot = new Robot(); |
| 107 | + robot.setAutoDelay(100); |
| 108 | + robot.delay(1000); |
| 109 | + robot.keyPress(KeyEvent.VK_CONTROL); |
| 110 | + robot.keyPress(KeyEvent.VK_A); |
| 111 | + robot.waitForIdle(); |
| 112 | + robot.keyRelease(KeyEvent.VK_A); |
| 113 | + robot.keyRelease(KeyEvent.VK_CONTROL); |
| 114 | + robot.waitForIdle(); |
| 115 | + robot.keyPress(KeyEvent.VK_ALT); |
| 116 | + robot.keyPress(KeyEvent.VK_F4); |
| 117 | + robot.waitForIdle(); |
| 118 | + robot.keyRelease(KeyEvent.VK_F4); |
| 119 | + robot.keyRelease(KeyEvent.VK_ALT); |
| 120 | + robot.waitForIdle(); |
| 121 | + robot.delay(1000); |
| 122 | + |
| 123 | + if (!actionFired || !closingWindowCalled) { |
| 124 | + throw new RuntimeException("Test FAILED(actionFired="+actionFired+ |
| 125 | + ";closingWindowCalled="+closingWindowCalled+")"); |
| 126 | + } |
| 127 | + } finally { |
| 128 | + if (frame != null) { |
| 129 | + frame.dispose(); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + }// class NoFocusOwnerAWTTest |
0 commit comments