Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JPopupMenu/6827786/bug6827786.java
38918 views
/*1* Copyright (c) 2007, 2013, 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*/2223/*24* @test25* @bug 682778626* @summary Tests duplicate mnemonics27* @author Peter Zhelezniakov28* @library ../../regtesthelpers29* @build Util30* @run main bug682778631*/32import java.awt.*;33import java.awt.event.KeyEvent;34import javax.swing.*;3536public class bug6827786 {3738private static JMenu menu;39private static Component focusable;4041public static void main(String[] args) throws Exception {42Robot robot = new Robot();43robot.setAutoDelay(50);4445SwingUtilities.invokeAndWait(new Runnable() {4647public void run() {48createAndShowGUI();49}50});5152robot.waitForIdle();5354SwingUtilities.invokeAndWait(new Runnable() {5556public void run() {57focusable.requestFocus();58}59});6061robot.waitForIdle();62checkfocus();6364// select menu65if (sun.awt.OSInfo.getOSType() == sun.awt.OSInfo.OSType.MACOSX) {66Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_F);67} else {68Util.hitKeys(robot, KeyEvent.VK_ALT, KeyEvent.VK_F);69}70// select submenu71Util.hitKeys(robot, KeyEvent.VK_S);72robot.waitForIdle();73// verify submenu is selected74verify(1);7576Util.hitKeys(robot, KeyEvent.VK_S);77robot.waitForIdle();78// verify last item is selected79verify(2);8081Util.hitKeys(robot, KeyEvent.VK_S);82robot.waitForIdle();83// selection should wrap to first item84verify(0);8586System.out.println("PASSED");8788}8990private static void checkfocus() throws Exception {91SwingUtilities.invokeAndWait(new Runnable() {9293public void run() {94if (!focusable.isFocusOwner()) {95throw new RuntimeException("Button is not the focus owner.");96}97}98});99}100101private static void verify(final int index) throws Exception {102SwingUtilities.invokeAndWait(new Runnable() {103104@Override105public void run() {106MenuElement[] path =107MenuSelectionManager.defaultManager().getSelectedPath();108MenuElement item = path[3];109if (item != menu.getMenuComponent(index)) {110System.err.println("Selected: " + item);111System.err.println("Should be: "112+ menu.getMenuComponent(index));113throw new RuntimeException("Test Failed");114}115}116});117}118119private static JMenuBar createMenuBar() {120menu = new JMenu("File");121menu.setMnemonic('F');122123menu.add(new JMenuItem("Save", 'S'));124125JMenu sub = new JMenu("Submenu");126sub.setMnemonic('S');127sub.add(new JMenuItem("Sub Item"));128menu.add(sub);129130menu.add(new JMenuItem("Special", 'S'));131132JMenuBar bar = new JMenuBar();133bar.add(menu);134return bar;135}136137private static void createAndShowGUI() {138JFrame frame = new JFrame("bug6827786");139frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);140frame.setJMenuBar(createMenuBar());141focusable = new JButton("Set Focus Here");142frame.add(focusable);143frame.pack();144frame.setVisible(true);145}146}147148149