test/jdk/javax/accessibility/6192422/bug6192422.java
changeset 54242 132590e72765
equal deleted inserted replaced
54241:3c369ad8c4ac 54242:132590e72765
       
     1 /*
       
     2  * Copyright (c) 2005, 2019, 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 import javax.accessibility.Accessible;
       
    25 import javax.accessibility.AccessibleContext;
       
    26 import javax.accessibility.AccessibleRole;
       
    27 import javax.swing.JFrame;
       
    28 import javax.swing.JMenu;
       
    29 import javax.swing.JMenuBar;
       
    30 import javax.swing.SwingUtilities;
       
    31 
       
    32 /**
       
    33  * @test
       
    34  * @bug 6192422 7106851
       
    35  * @key headful
       
    36  * @summary Verifies fix for JMenuBar not being in the accessibility hierarchy
       
    37  */
       
    38 public class bug6192422 {
       
    39 
       
    40     private static boolean foundJMenuBar = false;
       
    41 
       
    42     public static void main(String[] args) throws Throwable {
       
    43         SwingUtilities.invokeAndWait(new Runnable() {
       
    44             public void run() {
       
    45                 if (!testIt()) {
       
    46                     throw new RuntimeException("JMenuBar was not found");
       
    47                 }
       
    48             }
       
    49         });
       
    50     }
       
    51 
       
    52     /*
       
    53      * Test whether JMenuBar is in accessibility hierarchy
       
    54      */
       
    55     private static boolean testIt() {
       
    56 
       
    57         JFrame frame = new JFrame("bug6192422");
       
    58         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
    59 
       
    60         /*
       
    61          * Add a menu bar to the frame using setJMenuBar. The setJMenuBar
       
    62          * method add the menu bar to the JLayeredPane.
       
    63          */
       
    64         JMenuBar menuBar = new JMenuBar();
       
    65         menuBar.add(new JMenu("foo"));
       
    66         menuBar.add(new JMenu("bar"));
       
    67         menuBar.add(new JMenu("baz"));
       
    68         frame.setJMenuBar(menuBar);
       
    69 
       
    70         findJMenuBar(frame.getAccessibleContext());
       
    71         return foundJMenuBar;
       
    72     }
       
    73 
       
    74     /*
       
    75      * Finds the JMenuBar in the Accessibility hierarchy
       
    76      */
       
    77     private static void findJMenuBar(AccessibleContext ac) {
       
    78         if (ac != null) {
       
    79             System.err.println("findJMenuBar: ac = "+ac.getClass());
       
    80             int num = ac.getAccessibleChildrenCount();
       
    81             System.err.println("  #children "+num);
       
    82 
       
    83             for (int i = 0; i < num; i++) {
       
    84                 System.err.println("  child #"+i);
       
    85                 Accessible a = ac.getAccessibleChild(i);
       
    86                 AccessibleContext child = a.getAccessibleContext();
       
    87                 AccessibleRole role = child.getAccessibleRole();
       
    88                 System.err.println("  role "+role);
       
    89                 if (role == AccessibleRole.MENU_BAR) {
       
    90                     foundJMenuBar = true;
       
    91                     return;
       
    92                 }
       
    93                 if (child.getAccessibleChildrenCount() > 0) {
       
    94                     findJMenuBar(child);
       
    95                 }
       
    96             }
       
    97         }
       
    98     }
       
    99 }