1 /*
   2  * Copyright (c) 2006, 2018, 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 /*
  25   test
  26   @bug       6380743 8158380
  27   @summary   Submenu should be shown by mnemonic key press.
  28   @author    anton.tarasov@...: area=awt.focus
  29   @run       applet SubMenuShowTest.html
  30 */
  31 
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 import javax.swing.*;
  35 import java.applet.Applet;
  36 import java.util.concurrent.atomic.AtomicBoolean;
  37 import java.lang.reflect.InvocationTargetException;
  38 import test.java.awt.regtesthelpers.Util;
  39 import jdk.testlibrary.OSInfo;
  40 
  41 public class SubMenuShowTest extends Applet {
  42     Robot robot;
  43     JFrame frame = new JFrame("Test Frame");
  44     JMenuBar bar = new JMenuBar();
  45     JMenu menu = new JMenu("Menu");
  46     JMenu submenu = new JMenu("More");
  47     JMenuItem item = new JMenuItem("item");
  48     AtomicBoolean activated = new AtomicBoolean(false);
  49 
  50     public static void main(String[] args) {
  51         SubMenuShowTest app = new SubMenuShowTest();
  52         app.init();
  53         app.start();
  54     }
  55 
  56     public void init() {
  57         robot = Util.createRobot();
  58         robot.setAutoDelay(200);
  59         robot.setAutoWaitForIdle(true);
  60 
  61         // Create instructions for the user here, as well as set up
  62         // the environment -- set the layout manager, add buttons,
  63         // etc.
  64         this.setLayout (new BorderLayout ());
  65     }
  66 
  67     public void start() {
  68         menu.setMnemonic('f');
  69         submenu.setMnemonic('m');
  70         menu.add(submenu);
  71         submenu.add(item);
  72         bar.add(menu);
  73         frame.setJMenuBar(bar);
  74         frame.pack();
  75 
  76         item.addActionListener(new ActionListener() {
  77                 public void actionPerformed(ActionEvent e) {
  78                     System.out.println(e.toString());
  79                     synchronized (activated) {
  80                         activated.set(true);
  81                         activated.notifyAll();
  82                     }
  83                 }
  84             });
  85 
  86         frame.setVisible(true);
  87 
  88         boolean isMacOSX = (OSInfo.getOSType() == OSInfo.OSType.MACOSX);
  89         if (isMacOSX) {
  90             robot.keyPress(KeyEvent.VK_CONTROL);
  91         }
  92         robot.keyPress(KeyEvent.VK_ALT);
  93         robot.keyPress(KeyEvent.VK_F);
  94         robot.keyRelease(KeyEvent.VK_F);
  95         robot.keyRelease(KeyEvent.VK_ALT);
  96 
  97         if (isMacOSX) {
  98             robot.keyRelease(KeyEvent.VK_CONTROL);
  99         }
 100 
 101         robot.keyPress(KeyEvent.VK_M);
 102         robot.keyRelease(KeyEvent.VK_M);
 103         robot.keyPress(KeyEvent.VK_SPACE);
 104         robot.keyRelease(KeyEvent.VK_SPACE);
 105 
 106         if (!Util.waitForCondition(activated, 2000)) {
 107             throw new TestFailedException("a submenu wasn't activated by mnemonic key press");
 108         }
 109 
 110         System.out.println("Test passed.");
 111     }
 112 }
 113 
 114 class TestFailedException extends RuntimeException {
 115     TestFailedException(String msg) {
 116         super("Test failed: " + msg);
 117     }
 118 }
 119