Path: blob/master/test/jdk/javax/accessibility/6192422/bug6192422.java
41152 views
/*1* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.accessibility.Accessible;24import javax.accessibility.AccessibleContext;25import javax.accessibility.AccessibleRole;26import javax.swing.JFrame;27import javax.swing.JMenu;28import javax.swing.JMenuBar;29import javax.swing.SwingUtilities;3031/**32* @test33* @bug 6192422 710685134* @key headful35* @summary Verifies fix for JMenuBar not being in the accessibility hierarchy36*/37public class bug6192422 {3839private static boolean foundJMenuBar = false;4041public static void main(String[] args) throws Throwable {42SwingUtilities.invokeAndWait(new Runnable() {43public void run() {44if (!testIt()) {45throw new RuntimeException("JMenuBar was not found");46}47}48});49}5051/*52* Test whether JMenuBar is in accessibility hierarchy53*/54private static boolean testIt() {5556JFrame frame = new JFrame("bug6192422");57frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);5859/*60* Add a menu bar to the frame using setJMenuBar. The setJMenuBar61* method add the menu bar to the JLayeredPane.62*/63JMenuBar menuBar = new JMenuBar();64menuBar.add(new JMenu("foo"));65menuBar.add(new JMenu("bar"));66menuBar.add(new JMenu("baz"));67frame.setJMenuBar(menuBar);6869findJMenuBar(frame.getAccessibleContext());70return foundJMenuBar;71}7273/*74* Finds the JMenuBar in the Accessibility hierarchy75*/76private static void findJMenuBar(AccessibleContext ac) {77if (ac != null) {78System.err.println("findJMenuBar: ac = "+ac.getClass());79int num = ac.getAccessibleChildrenCount();80System.err.println(" #children "+num);8182for (int i = 0; i < num; i++) {83System.err.println(" child #"+i);84Accessible a = ac.getAccessibleChild(i);85AccessibleContext child = a.getAccessibleContext();86AccessibleRole role = child.getAccessibleRole();87System.err.println(" role "+role);88if (role == AccessibleRole.MENU_BAR) {89foundJMenuBar = true;90return;91}92if (child.getAccessibleChildrenCount() > 0) {93findJMenuBar(child);94}95}96}97}98}99100101