1 /*
2  * Copyright (c) 2019, 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  * @key headful
26  * @bug 8225505
27  * @summary CTRL + 1 does not show tooltip message for menu items
28  * @run main/manual JMenuItemToolTipKeyBindingsTest
29  */
30 
31 import javax.swing.JFrame;
32 import javax.swing.JMenu;
33 import javax.swing.JMenuBar;
34 import javax.swing.JMenuItem;
35 import javax.swing.SwingUtilities;
36 import java.awt.Button;
37 import java.awt.Dialog;
38 import java.awt.Panel;
39 import java.awt.TextArea;
40 import java.awt.event.ActionEvent;
41 import java.awt.event.ActionListener;
42 import java.util.concurrent.atomic.AtomicBoolean;
43 
44 public class JMenuItemToolTipKeyBindingsTest {
45     private static final long TIMEOUT = 5 * 60 * 1000;
46     private static final AtomicBoolean testCompleted = new AtomicBoolean(false);
47     private static volatile boolean testResult = false;
48 
49     private static Dialog controlDialog;
50     private static JFrame testFrame;
51 
52     private static final String instructions =
53             "Verify that \"CTRL\" + \"F1\" key sequence shows/hides tool tip message" +
54             "\nfor menu items.\n" +
55             "\n1. Open pop-up menu \"Menu\", (i.e. press \"F10\")." +
56             "\n2. Navigate to some menu element using keyboard." +
57             "\n3. Press \"CTRL\" + \"F1\" once menu item is selected." +
58             "\nIf tooltip message is displayed for the item then press \"Pass\"," +
59             "\n otherwise press \"Fail\".";
60 
main(String[] args)61     public static void main(String[] args) throws Exception {
62         try {
63             SwingUtilities.invokeAndWait(() -> createAndShowGUI());
64 
65             waitForCompleting();
66             if (!testResult) {
67                 throw new RuntimeException("Test FAILED!");
68             }
69         } finally {
70             if (controlDialog != null) {
71                 controlDialog.dispose();
72             }
73             if (testFrame != null) {
74                 testFrame.dispose();
75             }
76         }
77     }
78 
createAndShowGUI()79     private static void createAndShowGUI() {
80         controlDialog = new Dialog((JFrame)null, "JMenuItemToolTipKeyBindingsTest");
81 
82         TextArea messageArea = new TextArea(instructions, 15, 80, TextArea.SCROLLBARS_BOTH);
83         controlDialog.add("North", messageArea);
84 
85         Button passedButton = new Button("Pass");
86         passedButton.addActionListener(new ActionListener() {
87             @Override
88             public void actionPerformed(ActionEvent e) {
89                 testResult = true;
90                 completeTest();
91             }
92         });
93 
94         Button failedButton = new Button("Fail");
95         failedButton.addActionListener(new ActionListener() {
96             @Override
97             public void actionPerformed(ActionEvent e) {
98                 testResult = false;
99                 completeTest();
100             }
101         });
102 
103         Panel buttonPanel = new Panel();
104         buttonPanel.add("West",passedButton);
105         buttonPanel.add("East", failedButton);
106         controlDialog.add("South", buttonPanel);
107 
108         controlDialog.setBounds(250, 0, 500, 500);
109         controlDialog.setVisible(true);
110 
111         testFrame = new JFrame("JMenuItemToolTipKeyBindingsTest");
112         testFrame.setSize(200, 200);
113         JMenuBar jMenuBar = new JMenuBar();
114         JMenu jMenu = new JMenu("Menu");
115         for (int i = 0; i < 3; i++) {
116             JMenuItem jMenuItem = new JMenuItem("Item " + i);
117             jMenuItem.setToolTipText("Tooltip " + i);
118             jMenu.add(jMenuItem);
119         }
120         jMenuBar.add(jMenu);
121         testFrame.setJMenuBar(jMenuBar);
122         testFrame.setVisible(true);
123     }
124 
completeTest()125     private static void completeTest() {
126         testCompleted.set(true);
127         synchronized (testCompleted) {
128             testCompleted.notifyAll();
129         }
130     }
131 
waitForCompleting()132     private static void waitForCompleting() throws Exception {
133         synchronized (testCompleted) {
134             long startTime = System.currentTimeMillis();
135             while (!testCompleted.get()) {
136                 testCompleted.wait(TIMEOUT);
137                 if (System.currentTimeMillis() - startTime >= TIMEOUT) {
138                     break;
139                 }
140             }
141         }
142     }
143 }
144 
145