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