From ecec611af6c6314d7a834392f38468ad3f390e2d Mon Sep 17 00:00:00 2001
From: Abhishek Kumar <abhiscxk@openjdk.org>
Date: Mon, 3 Apr 2023 05:36:04 +0000
Subject: [PATCH] 8283404: [macos] a11y : Screen magnifier does not show JMenu
 name

Reviewed-by: serb
---
 .../libawt_lwawt/awt/a11y/MenuAccessibility.m |  10 +-
 .../JMenu/TestJMenuScreenMagnifier.java       | 105 ++++++++++++++++++
 2 files changed, 112 insertions(+), 3 deletions(-)
 create mode 100644 test/jdk/javax/accessibility/JMenu/TestJMenuScreenMagnifier.java

diff --git a/src/java.desktop/macosx/native/libawt_lwawt/awt/a11y/MenuAccessibility.m b/src/java.desktop/macosx/native/libawt_lwawt/awt/a11y/MenuAccessibility.m
index f9f13ca207f..e8b28d93068 100644
--- a/src/java.desktop/macosx/native/libawt_lwawt/awt/a11y/MenuAccessibility.m
+++ b/src/java.desktop/macosx/native/libawt_lwawt/awt/a11y/MenuAccessibility.m
@@ -32,9 +32,13 @@
 @implementation MenuAccessibility
 - (NSAccessibilityRole _Nonnull)accessibilityRole
 {
-        return [[[self parent] javaRole] isEqualToString:@"combobox"]
-               ? NSAccessibilityPopUpButtonRole
-               : NSAccessibilityMenuRole;
+        if ([[[self parent] javaRole] isEqualToString:@"combobox"]) {
+            return NSAccessibilityPopUpButtonRole;
+        } else if ([[[self parent] javaRole] isEqualToString:@"menubar"]) {
+            return NSAccessibilityMenuBarItemRole;
+        } else {
+            return NSAccessibilityMenuRole;
+        }
 }
 
 - (BOOL)isAccessibilityElement
diff --git a/test/jdk/javax/accessibility/JMenu/TestJMenuScreenMagnifier.java b/test/jdk/javax/accessibility/JMenu/TestJMenuScreenMagnifier.java
new file mode 100644
index 00000000000..da4a199e8c3
--- /dev/null
+++ b/test/jdk/javax/accessibility/JMenu/TestJMenuScreenMagnifier.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2023, 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 8283404
+ * @library /java/awt/regtesthelpers
+ * @build PassFailJFrame
+ * @requires (os.family == "mac")
+ * @summary Verifies if JMenu accessibility label magnifies using
+ * screen magnifier a11y tool.
+ * @run main/manual TestJMenuScreenMagnifier
+ */
+
+import java.lang.reflect.InvocationTargetException;
+import javax.swing.JFrame;
+import javax.swing.JMenu;
+import javax.swing.JMenuBar;
+import javax.swing.JMenuItem;
+import javax.swing.SwingUtilities;
+
+public class TestJMenuScreenMagnifier {
+    private static JFrame frame;
+    private static final String INSTRUCTIONS =
+            "1) Enable Screen magnifier on theMac \n\n" +
+                "System Preference -> Accessibility -> Zoom -> " +
+                "Select ( Enable Hover Text) \n\n" +
+            "2) Move the mouse over the \"File\" or \"Edit\" menu by pressing  " +
+                "\"cmd\" button.\n\n" +
+            "3) If magnified label is visible, Press Pass else Fail.";
+
+    public static void main(String[] args) throws InterruptedException,
+             InvocationTargetException {
+        PassFailJFrame passFailJFrame = new PassFailJFrame(
+                "JMenu Screen Magnifier Test Instructions", INSTRUCTIONS, 5, 12, 40);
+        try {
+            SwingUtilities.invokeAndWait(
+                    TestJMenuScreenMagnifier::createAndShowUI);
+            passFailJFrame.awaitAndCheck();
+        } finally {
+            SwingUtilities.invokeAndWait(() -> {
+                if (frame != null) {
+                    frame.dispose();
+                }
+            });
+        }
+    }
+    private static void createAndShowUI() {
+        frame = new JFrame("JMenu A11Y Screen Magnifier Test");
+
+        JMenu file = new JMenu("File");
+        file.getAccessibleContext().setAccessibleName("File Menu");
+
+        JMenuItem open = new JMenuItem("Open");
+        open.getAccessibleContext().setAccessibleName("Open MenuItem");
+
+        JMenuItem quit = new JMenuItem("Quit");
+        quit.getAccessibleContext().setAccessibleName("Quit MenuItem");
+
+        file.add(open);
+        file.add(quit);
+
+        JMenu edit = new JMenu("Edit");
+        edit.getAccessibleContext().setAccessibleName("Edit Menu");
+
+        JMenuItem cut = new JMenuItem("Cut");
+        cut.getAccessibleContext().setAccessibleName("Cut MenuItem");
+
+        edit.add(cut);
+
+        JMenuBar jMenuBar = new JMenuBar();
+
+        jMenuBar.add(file);
+        jMenuBar.add(edit);
+
+
+        PassFailJFrame.addTestWindow(frame);
+        PassFailJFrame.positionTestWindow(frame,
+                PassFailJFrame.Position.HORIZONTAL);
+        frame.setJMenuBar(jMenuBar);
+        frame.setSize(300, 100);
+        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+        frame.setVisible(true);
+    }
+}