1 /*
   2  * Copyright (c) 2013, 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 /* @test
  25    @bug 4983388 8015600
  26    @summary shortcuts on menus do not work on JDS
  27    @author Oleg Mokhovikov
  28    @library ../../../../regtesthelpers
  29    @build Util
  30    @run main bug4983388
  31 */
  32 
  33 import sun.awt.*;
  34 import java.awt.*;
  35 import javax.swing.*;
  36 import javax.swing.event.MenuListener;
  37 import javax.swing.event.MenuEvent;
  38 import java.awt.event.KeyEvent;
  39 
  40 public class bug4983388 {
  41     static volatile boolean bMenuSelected = false;
  42 
  43     private static class TestMenuListener implements MenuListener {
  44         public void menuCanceled(MenuEvent e) {}
  45         public void menuDeselected(MenuEvent e) {}
  46         public void menuSelected(MenuEvent e) {
  47             System.out.println("menuSelected");
  48             bMenuSelected = true;
  49         }
  50     }
  51 
  52     private static void createAndShowGUI() {
  53         JMenuBar menuBar = new JMenuBar();
  54         JMenu menu = new JMenu("File");
  55         menu.setMnemonic('F');
  56         menuBar.add(menu);
  57         JFrame frame = new JFrame();
  58         frame.setJMenuBar(menuBar);
  59         frame.pack();
  60         frame.setVisible(true);
  61         MenuListener listener = new TestMenuListener();
  62         menu.addMenuListener(listener);
  63     }
  64 
  65     public static void main(String[] args) throws Exception {
  66 
  67         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  68         try {
  69             UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
  70         } catch (UnsupportedLookAndFeelException | ClassNotFoundException ex) {
  71             System.err.println("GTKLookAndFeel is not supported on this platform. Using defailt LaF for this platform.");
  72         }
  73 
  74         SwingUtilities.invokeAndWait(new Runnable() {
  75             public void run() {
  76                 createAndShowGUI();
  77             }
  78         });
  79 
  80         Robot robot = new Robot();
  81         Util.hitMnemonics(robot, KeyEvent.VK_F);
  82 
  83         toolkit.realSync();
  84 
  85         if (!bMenuSelected) {
  86             throw new RuntimeException("shortcuts on menus do not work");
  87         }
  88     }
  89 }