6827800: Default button is activated even when it is invisible
authoraghaisas
Thu, 26 May 2016 17:01:24 +0530
changeset 39002 5538b16a27af
parent 39001 d446b5342c35
child 39003 597412b25729
6827800: Default button is activated even when it is invisible Reviewed-by: serb, rchamyal
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicRootPaneUI.java
jdk/test/javax/swing/plaf/basic/BasicRootPaneUI/HiddenDefaultButtonTest.java
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicRootPaneUI.java	Thu May 26 16:02:19 2016 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicRootPaneUI.java	Thu May 26 17:01:24 2016 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -281,7 +281,7 @@
 
             if (sender instanceof JRootPane) {
                 JButton owner = ((JRootPane)sender).getDefaultButton();
-                return (owner != null && owner.getModel().isEnabled());
+                return (owner != null && owner.getModel().isEnabled() && owner.isShowing());
             }
             return true;
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/plaf/basic/BasicRootPaneUI/HiddenDefaultButtonTest.java	Thu May 26 17:01:24 2016 +0530
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @bug 6827800
+ * @summary Test to check hidden default button does not respond to 'Enter' key
+ * @run main HiddenDefaultButtonTest
+ */
+
+import java.awt.AWTException;
+import java.awt.Robot;
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.SwingUtilities;
+
+public class HiddenDefaultButtonTest {
+
+    private static int ButtonClickCount = 0;
+    private static JFrame frame;
+
+    private static void createGUI() {
+        frame = new JFrame();
+        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+        JButton button = new JButton("Default button");
+        button.setDefaultCapable(true);
+        button.addActionListener(new ActionListener() {
+            public void actionPerformed(ActionEvent e) {
+                ButtonClickCount++;
+            }
+        });
+
+        frame.add(button);
+        button.setVisible(false);
+
+        frame.getRootPane().setDefaultButton(button);
+
+        frame.setSize(200, 200);
+        frame.setLocationRelativeTo(null);
+        frame.setVisible(true);
+    }
+
+    private static void disposeTestUI() throws Exception {
+        SwingUtilities.invokeAndWait(() -> {
+            frame.dispose();
+        });
+    }
+
+    private static void test() throws Exception {
+        // Create Robot
+        Robot testRobot = new Robot();
+
+        testRobot.waitForIdle();
+
+        testRobot.keyPress(KeyEvent.VK_ENTER);
+        testRobot.delay(20);
+        testRobot.keyRelease(KeyEvent.VK_ENTER);
+        testRobot.delay(200);
+        testRobot.keyPress(KeyEvent.VK_ENTER);
+        testRobot.delay(20);
+        testRobot.keyRelease(KeyEvent.VK_ENTER);
+
+        testRobot.waitForIdle();
+
+        if (ButtonClickCount != 0) {
+            disposeTestUI();
+            throw new RuntimeException("DefaultButton is pressed even if it is invisible");
+        }
+
+    }
+
+    public static void main(String[] args) throws Exception {
+        // create UI
+        SwingUtilities.invokeAndWait(new Runnable() {
+            public void run() {
+                HiddenDefaultButtonTest.createGUI();
+            }
+        });
+
+        // Test default button press by pressing EnterKey using Robot
+        test();
+
+        // dispose UI
+        HiddenDefaultButtonTest.disposeTestUI();
+    }
+}
+