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 
  39 import jdk.test.lib.Platform;
  40 import test.java.awt.regtesthelpers.Util;
  41 
  42 public class SubMenuShowTest extends Applet {
  43     Robot robot;
  44     JFrame frame = new JFrame("Test Frame");
  45     JMenuBar bar = new JMenuBar();
  46     JMenu menu = new JMenu("Menu");
  47     JMenu submenu = new JMenu("More");
  48     JMenuItem item = new JMenuItem("item");
  49     AtomicBoolean activated = new AtomicBoolean(false);
  50 
  51     public static void main(String[] args) {
  52         SubMenuShowTest app = new SubMenuShowTest();
  53         app.init();
  54         app.start();
  55     }
  56 
  57     public void init() {
  58         robot = Util.createRobot();
  59         robot.setAutoDelay(200);
  60         robot.setAutoWaitForIdle(true);
  61 
  62         // Create instructions for the user here, as well as set up
  63         // the environment -- set the layout manager, add buttons,
  64         // etc.
  65         this.setLayout (new BorderLayout ());
  66     }
  67 
  68     public void start() {
  69         menu.setMnemonic('f');
  70         submenu.setMnemonic('m');
  71         menu.add(submenu);
  72         submenu.add(item);
  73         bar.add(menu);
  74         frame.setJMenuBar(bar);
  75         frame.pack();
  76 
  77         item.addActionListener(new ActionListener() {
  78                 public void actionPerformed(ActionEvent e) {
  79                     System.out.println(e.toString());
  80                     synchronized (activated) {
  81                         activated.set(true);
  82                         activated.notifyAll();
  83                     }
  84                 }
  85             });
  86 
  87         frame.setVisible(true);
  88 
  89         boolean isMacOSX = Platform.isOSX();
  90         if (isMacOSX) {
  91             robot.keyPress(KeyEvent.VK_CONTROL);
  92         }
  93         robot.keyPress(KeyEvent.VK_ALT);
  94         robot.keyPress(KeyEvent.VK_F);
  95         robot.keyRelease(KeyEvent.VK_F);
  96         robot.keyRelease(KeyEvent.VK_ALT);
  97 
  98         if (isMacOSX) {
  99             robot.keyRelease(KeyEvent.VK_CONTROL);
 100         }
 101 
 102         robot.keyPress(KeyEvent.VK_M);
 103         robot.keyRelease(KeyEvent.VK_M);
 104         robot.keyPress(KeyEvent.VK_SPACE);
 105         robot.keyRelease(KeyEvent.VK_SPACE);
 106 
 107         if (!Util.waitForCondition(activated, 2000)) {
 108             throw new TestFailedException("a submenu wasn't activated by mnemonic key press");
 109         }
 110 
 111         System.out.println("Test passed.");
 112     }
 113 }
 114 
 115 class TestFailedException extends RuntimeException {
 116     TestFailedException(String msg) {
 117         super("Test failed: " + msg);
 118     }
 119 }
 120