Path: blob/master/test/jdk/javax/swing/JPopupMenu/6827786/bug6827786.java
41153 views
/*1* Copyright (c) 2007, 2018, 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* @key headful26* @bug 682778627* @summary Tests duplicate mnemonics28* @author Peter Zhelezniakov29* @library ../../regtesthelpers30* @library /test/lib31* @modules java.desktop/sun.awt32* @build jdk.test.lib.Platform33* @build Util34* @run main bug682778635*/3637import jdk.test.lib.Platform;38import java.awt.*;39import java.awt.event.KeyEvent;40import javax.swing.*;4142public class bug6827786 {4344private static JMenu menu;45private static Component focusable;46private static JFrame frame;4748public static void main(String[] args) throws Exception {49try {50Robot robot = new Robot();51robot.setAutoDelay(100);52// move mouse outside menu to prevent auto selection53robot.mouseMove(100,100);54robot.waitForIdle();5556SwingUtilities.invokeAndWait(new Runnable() {5758public void run() {59createAndShowGUI();60}61});6263robot.waitForIdle();64robot.delay(1000);6566SwingUtilities.invokeAndWait(new Runnable() {6768public void run() {69focusable.requestFocus();70}71});7273robot.waitForIdle();74checkfocus();7576// select menu77if (Platform.isOSX()) {78Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_F);79} else {80Util.hitKeys(robot, KeyEvent.VK_ALT, KeyEvent.VK_F);81}82// select submenu83Util.hitKeys(robot, KeyEvent.VK_S);84robot.waitForIdle();85// verify submenu is selected86verify(1);8788Util.hitKeys(robot, KeyEvent.VK_S);89robot.waitForIdle();90// verify last item is selected91verify(2);9293Util.hitKeys(robot, KeyEvent.VK_S);94robot.waitForIdle();95// selection should wrap to first item96verify(0);9798System.out.println("PASSED");99} finally {100if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());101}102}103104private static void checkfocus() throws Exception {105SwingUtilities.invokeAndWait(new Runnable() {106107public void run() {108if (!focusable.isFocusOwner()) {109throw new RuntimeException("Button is not the focus owner.");110}111}112});113}114115private static void verify(final int index) throws Exception {116SwingUtilities.invokeAndWait(new Runnable() {117118@Override119public void run() {120MenuElement[] path =121MenuSelectionManager.defaultManager().getSelectedPath();122MenuElement item = path[3];123if (item != menu.getMenuComponent(index)) {124System.err.println("Selected: " + item);125System.err.println("Should be: "126+ menu.getMenuComponent(index));127throw new RuntimeException("Test Failed");128}129}130});131}132133private static JMenuBar createMenuBar() {134menu = new JMenu("File");135menu.setMnemonic('F');136137menu.add(new JMenuItem("Save", 'S'));138139JMenu sub = new JMenu("Submenu");140sub.setMnemonic('S');141sub.add(new JMenuItem("Sub Item"));142menu.add(sub);143144menu.add(new JMenuItem("Special", 'S'));145146JMenuBar bar = new JMenuBar();147bar.add(menu);148return bar;149}150151private static void createAndShowGUI() {152frame = new JFrame("bug6827786");153frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);154frame.setJMenuBar(createMenuBar());155focusable = new JButton("Set Focus Here");156frame.add(focusable);157frame.pack();158frame.setLocationRelativeTo(null);159frame.setVisible(true);160}161}162163164