jdk/test/javax/swing/JMenuItem/ShortcutNotDiplayed/ShortcutNotDisplayedTest.java
changeset 14314 9d9a66a5d6da
child 26750 d0d6c64a2e2b
equal deleted inserted replaced
14313:4a3ba762d989 14314:9d9a66a5d6da
       
     1 /*
       
     2  * Copyright (c) 2012, 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 7186371
       
    27  * @summary [macosx] Main menu shortcuts not displayed
       
    28  * @author vera.akulova@oracle.com
       
    29  * @run main/manual ShortcutNotDisplayedTest
       
    30  */
       
    31 
       
    32 import java.awt.*;
       
    33 import java.awt.event.*;
       
    34 import javax.swing.*;
       
    35 
       
    36 public class ShortcutNotDisplayedTest {
       
    37     static volatile boolean done = false;
       
    38     static volatile boolean pass = false;
       
    39     static final String PASS_COMMAND = "pass";
       
    40 
       
    41     public static void main(String[] args) throws Exception {
       
    42         if (sun.awt.OSInfo.getOSType() != sun.awt.OSInfo.OSType.MACOSX) {
       
    43             System.out.println("This test is for MacOS only. Automatically passed on other platforms.");
       
    44             return;
       
    45         }
       
    46         System.setProperty("apple.laf.useScreenMenuBar", "true");
       
    47         SwingUtilities.invokeAndWait(new Runnable() {
       
    48             public void run() {
       
    49                 createAndShowGUI();
       
    50             }
       
    51         });
       
    52 
       
    53         do { try { Thread.sleep(300); } catch (Exception e) {} } while (!done) ;
       
    54         if (!pass) {
       
    55             throw new Exception("Shortcuts not displayed as expected in the screen menu bar.");
       
    56         }
       
    57     }
       
    58 
       
    59     private static void createAndShowGUI() {
       
    60         JMenuItem newItem = new JMenuItem("Exit");
       
    61         newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, java.awt.event.InputEvent.META_MASK));
       
    62 
       
    63         JMenu menu = new JMenu("Test Frame Window Menu");
       
    64         menu.setMnemonic(KeyEvent.VK_M);
       
    65         menu.add(newItem);
       
    66 
       
    67         JMenuBar bar = new JMenuBar();
       
    68         bar.add(menu);
       
    69         JTextArea text = new JTextArea(
       
    70             "  Please follow instructions:\n" +
       
    71             "  1. You should see \"Test Frame Window Menu\" menu on the screen menu bar.\n" +
       
    72             "  2. Open \"Test Frame Window Menu\" menu. \n" +
       
    73             "     Check that menu item \"Exit\" has a shortcut with image for Command Key and symbol \"E\". \n" +
       
    74             "     If you see the shortcut press \"Passed\". Otherwise press \"Failed\".\n"
       
    75         );
       
    76         text.setEditable(false);
       
    77 
       
    78         JScrollPane sp = new JScrollPane(text);
       
    79         sp.setSize(300,200);
       
    80 
       
    81         JButton passBtn = new JButton("Pass");
       
    82         passBtn.setActionCommand(PASS_COMMAND);
       
    83         JButton failBtn = new JButton("Fail");
       
    84         ActionListener listener = new ActionListener() {
       
    85             public void actionPerformed(ActionEvent e) {
       
    86                 if (e.getActionCommand().equals(PASS_COMMAND)) {
       
    87                     pass = true;
       
    88                 }
       
    89                 done = true;
       
    90             }
       
    91         };
       
    92 
       
    93         JFrame testFrame = new JFrame("Test Frame Window");
       
    94         testFrame.setLayout(new FlowLayout());
       
    95         testFrame.setBounds(100, 100, 600, 180);
       
    96         testFrame.setJMenuBar(bar);
       
    97         testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
    98         passBtn.addActionListener(listener);
       
    99         failBtn.addActionListener(listener);
       
   100         testFrame.getContentPane().add(sp);
       
   101         testFrame.getContentPane().add(passBtn);
       
   102         testFrame.getContentPane().add(failBtn);
       
   103         testFrame.setVisible(true);
       
   104     }
       
   105 }