Path: blob/master/test/jdk/java/awt/KeyboardFocusmanager/TypeAhead/SubMenuShowTest/SubMenuShowTest.java
42819 views
/*1* Copyright (c) 2006, 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 6380743 8158380 819862427@summary Submenu should be shown by mnemonic key press.28@author anton.tarasov@...: area=awt.focus29@library ../../../regtesthelpers30@library /test/lib31@build Util32@build jdk.test.lib.Platform33@run main SubMenuShowTest34*/3536import java.awt.Robot;37import java.awt.BorderLayout;38import java.awt.event.KeyEvent;39import java.awt.event.ActionListener;40import java.awt.event.ActionEvent;41import javax.swing.SwingUtilities;42import javax.swing.JFrame;43import javax.swing.JMenuBar;44import javax.swing.JMenu;45import javax.swing.JMenuItem;46import java.util.concurrent.atomic.AtomicBoolean;47import jdk.test.lib.Platform;48import test.java.awt.regtesthelpers.Util;4950public class SubMenuShowTest {51private static Robot robot;52private static JFrame frame;53private static JMenuBar bar;54private static JMenu menu;55private static JMenu submenu;56private static JMenuItem item;57private static AtomicBoolean activated = new AtomicBoolean(false);5859public static void main(String[] args) throws Exception {60SwingUtilities.invokeAndWait(SubMenuShowTest::createAndShowGUI);6162try {63robot = new Robot();64robot.setAutoDelay(100);65robot.setAutoWaitForIdle(true);6667doTest();68} catch (Exception ex) {69throw new RuntimeException("Test failed: Exception thrown:"+ex);70} finally {71dispose();72}7374System.out.println("Test passed.");75}7677public static void dispose() throws Exception {78if(frame != null) {79SwingUtilities.invokeAndWait(() -> {80frame.dispose();81});82}83}8485public static void createAndShowGUI() {86// Create instructions for the user here, as well as set up87// the environment -- set the layout manager, add buttons,88// etc.89frame = new JFrame("Test Frame");90bar = new JMenuBar();91menu = new JMenu("Menu");92submenu = new JMenu("More");93item = new JMenuItem("item");9495frame.setLayout (new BorderLayout ());96menu.setMnemonic('f');97submenu.setMnemonic('m');98menu.add(submenu);99submenu.add(item);100bar.add(menu);101frame.setJMenuBar(bar);102frame.pack();103104item.addActionListener(new ActionListener() {105public void actionPerformed(ActionEvent e) {106System.out.println(e.toString());107synchronized (activated) {108activated.set(true);109activated.notifyAll();110}111}112});113114frame.setVisible(true);115}116117public static void doTest() {118boolean isMacOSX = Platform.isOSX();119if (isMacOSX) {120robot.keyPress(KeyEvent.VK_CONTROL);121}122robot.keyPress(KeyEvent.VK_ALT);123robot.keyPress(KeyEvent.VK_F);124robot.keyRelease(KeyEvent.VK_F);125robot.keyRelease(KeyEvent.VK_ALT);126127if (isMacOSX) {128robot.keyRelease(KeyEvent.VK_CONTROL);129}130131robot.keyPress(KeyEvent.VK_M);132robot.keyRelease(KeyEvent.VK_M);133robot.keyPress(KeyEvent.VK_SPACE);134robot.keyRelease(KeyEvent.VK_SPACE);135136if (!Util.waitForCondition(activated, 1500)) {137throw new TestFailedException("A submenu wasn't activated by mnemonic key press");138}139}140}141142class TestFailedException extends RuntimeException {143TestFailedException(String msg) {144super("Test failed: " + msg);145}146}147148149