8178448: MenuBar item handler fired twice
authorazvegint
Thu, 10 Aug 2017 10:41:04 +0530
changeset 47173 948b0623c6d6
parent 47172 ced0784a8640
child 47174 9ccac4332a21
8178448: MenuBar item handler fired twice Reviewed-by: serb
jdk/src/java.desktop/share/classes/java/awt/Desktop.java
jdk/test/javax/swing/JMenuItem/ActionListenerCalledTwice/ActionListenerCalledTwiceTest.java
--- a/jdk/src/java.desktop/share/classes/java/awt/Desktop.java	Tue Aug 08 14:07:07 2017 +0100
+++ b/jdk/src/java.desktop/share/classes/java/awt/Desktop.java	Thu Aug 10 10:41:04 2017 +0530
@@ -1002,6 +1002,15 @@
     public void setDefaultMenuBar(final JMenuBar menuBar) {
         checkEventsProcessingPermission();
         checkActionSupport(Action.APP_MENU_BAR);
+
+        if (menuBar != null) {
+            Container parent = menuBar.getParent();
+            if (parent != null) {
+                parent.remove(menuBar);
+                menuBar.updateUI();
+            }
+        }
+
         peer.setDefaultMenuBar(menuBar);
     }
 
--- a/jdk/test/javax/swing/JMenuItem/ActionListenerCalledTwice/ActionListenerCalledTwiceTest.java	Tue Aug 08 14:07:07 2017 +0100
+++ b/jdk/test/javax/swing/JMenuItem/ActionListenerCalledTwice/ActionListenerCalledTwiceTest.java	Thu Aug 10 10:41:04 2017 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2017, 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
@@ -23,9 +23,10 @@
 
  /*
  * @test
- * @bug 7160951 8152492
+ * @bug 7160951 8152492 8178448
  * @summary [macosx] ActionListener called twice for JMenuItem using ScreenMenuBar
  * @author vera.akulova@oracle.com
+ * @modules java.desktop/java.awt:open
  * @library ../../../../lib/testlibrary
  * @build jdk.testlibrary.OSInfo
  * @run main ActionListenerCalledTwiceTest
@@ -37,7 +38,8 @@
 
 public class ActionListenerCalledTwiceTest {
 
-    static String menuItems[] = {"Item1", "Item2", "Item3", "Item4", "Item5", "Item6"};
+    static String menuItems[] = {"Item1", "Item2", "Item3",
+                                    "Item4", "Item5", "Item6"};
     static KeyStroke keyStrokes[] = {
         KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.META_MASK),
         KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),
@@ -46,74 +48,50 @@
         KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK),
         KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, InputEvent.META_MASK)
     };
-    static JMenu menu;
+
+    static JMenuBar bar;
     static JFrame frame;
     static volatile int listenerCallCounter = 0;
 
     public static void main(String[] args) throws Exception {
         if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
-            System.out.println("This test is for MacOS only. Automatically passed on other platforms.");
+            System.out.println("This test is for MacOS only." +
+                    " Automatically passed on other platforms.");
             return;
         }
 
-        System.setProperty("apple.laf.useScreenMenuBar", "true");
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                createAndShowGUI();
-            }
-        });
+        try {
 
-        Robot robot = new Robot();
-        robot.setAutoDelay(100);
+            System.setProperty("apple.laf.useScreenMenuBar", "true");
+            SwingUtilities.invokeAndWait(
+                    ActionListenerCalledTwiceTest::createAndShowGUI);
 
-        for (int i = 0; i < menuItems.length; ++i) {
-            KeyStroke ks = keyStrokes[i];
-            int modKeyCode = getModKeyCode(ks.getModifiers());
-
-            if (modKeyCode != 0) {
-                robot.keyPress(modKeyCode);
-            }
+            Robot robot = new Robot();
+            robot.setAutoDelay(100);
 
-            robot.keyPress(ks.getKeyCode());
-            robot.keyRelease(ks.getKeyCode());
+            testForTwice(robot, "");
 
-            if (modKeyCode != 0) {
-                robot.keyRelease(modKeyCode);
-            }
-
+            SwingUtilities.invokeAndWait(
+                    ActionListenerCalledTwiceTest::testDefaultMenuBar);
             robot.waitForIdle();
 
-            if (listenerCallCounter != 1) {
-                throw new Exception("Test failed: ActionListener for " + menuItems[i]
-                        + " called " + listenerCallCounter + " times instead of 1!");
-            }
-
-            listenerCallCounter = 0;
+            testForTwice(robot, "DefaultMenuBar");
+        } finally {
+            SwingUtilities.invokeAndWait(() -> frame.dispose());
         }
-        SwingUtilities.invokeAndWait(new Runnable() {
-            public void run() {
-                frame.dispose();
-            }
-        });
     }
 
     private static void createAndShowGUI() {
-        menu = new JMenu("Menu");
+        JMenu menu = new JMenu("Menu");
 
         for (int i = 0; i < menuItems.length; ++i) {
             JMenuItem newItem = new JMenuItem(menuItems[i]);
             newItem.setAccelerator(keyStrokes[i]);
-            newItem.addActionListener(
-                    new ActionListener() {
-                public void actionPerformed(ActionEvent e) {
-                    listenerCallCounter++;
-                }
-            }
-            );
+            newItem.addActionListener(e -> listenerCallCounter++);
             menu.add(newItem);
         }
 
-        JMenuBar bar = new JMenuBar();
+        bar = new JMenuBar();
         bar.add(menu);
         frame = new JFrame("Test");
         frame.setJMenuBar(bar);
@@ -141,4 +119,40 @@
 
         return 0;
     }
+
+    private static void testForTwice(Robot robot, String exceptionPrefix)
+                                        throws Exception{
+        for (int i = 0; i < menuItems.length; ++i) {
+            KeyStroke ks = keyStrokes[i];
+            int modKeyCode = getModKeyCode(ks.getModifiers());
+
+            if (modKeyCode != 0) {
+                robot.keyPress(modKeyCode);
+            }
+
+            robot.keyPress(ks.getKeyCode());
+            robot.keyRelease(ks.getKeyCode());
+
+            if (modKeyCode != 0) {
+                robot.keyRelease(modKeyCode);
+            }
+
+            robot.waitForIdle();
+
+            if (listenerCallCounter != 1) {
+                throw new Exception(exceptionPrefix
+                        + " Test failed: ActionListener for " + menuItems[i]
+                        + " called " + listenerCallCounter + " times instead of 1!");
+            }
+
+            listenerCallCounter = 0;
+        }
+    }
+
+    private static void testDefaultMenuBar() {
+        if (Desktop.getDesktop().isSupported(Desktop.Action.APP_MENU_BAR)) {
+            Desktop.getDesktop().setDefaultMenuBar(bar);
+            frame.setExtendedState(JFrame.ICONIFIED);
+        }
+    }
 }