1 /*
2  * Copyright (c) 2016, 2017, 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 8149456 8147834 8150230 8155740 8163265
28  * @requires os.family == "mac"
29  * @summary Tests key codes for all keys supported in Java for Mac OS X.
30  * @run main AllKeyCode
31  */
32 
33 import java.awt.AWTException;
34 import java.awt.Robot;
35 import java.awt.event.KeyEvent;
36 import java.awt.event.KeyListener;
37 import java.awt.Frame;
38 import java.awt.TextArea;
39 
40 public class AllKeyCode extends Frame {
41 
42     private static Frame frame;
43     private static TextArea textArea;
44     private static KeyListener keyListener;
45     private static int allKeyArr[];
46     private static int keyPressedIndex;
47 
AllKeyCode()48     AllKeyCode() {
49         AllKeyCode.allKeyArr = new int[] {
50             KeyEvent.VK_BACK_SPACE,
51             KeyEvent.VK_TAB,
52             KeyEvent.VK_ENTER,
53             KeyEvent.VK_CLEAR,
54             KeyEvent.VK_SHIFT,
55             KeyEvent.VK_CONTROL,
56             KeyEvent.VK_ALT,
57             KeyEvent.VK_CAPS_LOCK,
58             KeyEvent.VK_ESCAPE,
59             KeyEvent.VK_SPACE,
60             KeyEvent.VK_PAGE_UP,
61             KeyEvent.VK_PAGE_DOWN,
62             KeyEvent.VK_END,
63             KeyEvent.VK_HOME,
64             KeyEvent.VK_LEFT,
65             KeyEvent.VK_UP,
66             KeyEvent.VK_RIGHT,
67             KeyEvent.VK_DOWN,
68             KeyEvent.VK_COMMA,
69             KeyEvent.VK_MINUS,
70             KeyEvent.VK_PERIOD,
71             KeyEvent.VK_SLASH,
72             KeyEvent.VK_0,
73             KeyEvent.VK_1,
74             KeyEvent.VK_2,
75             KeyEvent.VK_3,
76             KeyEvent.VK_4,
77             KeyEvent.VK_5,
78             KeyEvent.VK_6,
79             KeyEvent.VK_7,
80             KeyEvent.VK_8,
81             KeyEvent.VK_9,
82             KeyEvent.VK_SEMICOLON,
83             KeyEvent.VK_EQUALS,
84             KeyEvent.VK_A,
85             KeyEvent.VK_B,
86             KeyEvent.VK_C,
87             KeyEvent.VK_D,
88             KeyEvent.VK_E,
89             KeyEvent.VK_F,
90             KeyEvent.VK_G,
91             KeyEvent.VK_H,
92             KeyEvent.VK_I,
93             KeyEvent.VK_J,
94             KeyEvent.VK_K,
95             KeyEvent.VK_L,
96             KeyEvent.VK_M,
97             KeyEvent.VK_N,
98             KeyEvent.VK_O,
99             KeyEvent.VK_P,
100             KeyEvent.VK_Q,
101             KeyEvent.VK_R,
102             KeyEvent.VK_S,
103             KeyEvent.VK_T,
104             KeyEvent.VK_U,
105             KeyEvent.VK_V,
106             KeyEvent.VK_W,
107             KeyEvent.VK_X,
108             KeyEvent.VK_Y,
109             KeyEvent.VK_Z,
110             KeyEvent.VK_OPEN_BRACKET,
111             KeyEvent.VK_BACK_SLASH,
112             KeyEvent.VK_CLOSE_BRACKET,
113             KeyEvent.VK_NUMPAD0,
114             KeyEvent.VK_NUMPAD1,
115             KeyEvent.VK_NUMPAD2,
116             KeyEvent.VK_NUMPAD3,
117             KeyEvent.VK_NUMPAD4,
118             KeyEvent.VK_NUMPAD5,
119             KeyEvent.VK_NUMPAD6,
120             KeyEvent.VK_NUMPAD7,
121             KeyEvent.VK_NUMPAD8,
122             KeyEvent.VK_NUMPAD9,
123             KeyEvent.VK_MULTIPLY,
124             KeyEvent.VK_ADD,
125             KeyEvent.VK_SUBTRACT,
126             KeyEvent.VK_DECIMAL,
127             KeyEvent.VK_DIVIDE,
128             KeyEvent.VK_F1,
129             KeyEvent.VK_F2,
130             KeyEvent.VK_F3,
131             KeyEvent.VK_F4,
132             KeyEvent.VK_F5,
133             KeyEvent.VK_F6,
134             KeyEvent.VK_F7,
135             KeyEvent.VK_F8,
136             KeyEvent.VK_F9,
137             KeyEvent.VK_F10,
138             KeyEvent.VK_F11,
139             KeyEvent.VK_F12,
140             KeyEvent.VK_DELETE,
141             KeyEvent.VK_HELP,
142             KeyEvent.VK_META,
143             KeyEvent.VK_BACK_QUOTE,
144             KeyEvent.VK_QUOTE,
145             KeyEvent.VK_F13,
146             KeyEvent.VK_F14,
147             KeyEvent.VK_F15,
148             KeyEvent.VK_F16,
149             KeyEvent.VK_F17,
150             KeyEvent.VK_F18,
151             KeyEvent.VK_F19,
152             KeyEvent.VK_F20,
153             KeyEvent.VK_ALT_GRAPH
154         };
155 
156         keyPressedIndex = -1;
157     }
158 
createAndShowGUI()159     private void createAndShowGUI() {
160         frame = new Frame("Function Key Keycodes");
161         textArea = new TextArea();
162         textArea.setFocusable(true);
163         frame.add(textArea);
164         frame.pack();
165         frame.setSize(200, 200);
166 
167         textArea.addKeyListener(keyListener = new KeyListener() {
168 
169             @Override
170             public void keyTyped(KeyEvent ke) {
171             }
172 
173             @Override
174             public void keyPressed(KeyEvent ke) {
175                 if (allKeyArr[keyPressedIndex] != ke.getKeyCode()) {
176                     throw new RuntimeException("Wrong keycode received");
177                 }
178             }
179 
180             @Override
181             public void keyReleased(KeyEvent ke) {
182             }
183         });
184         frame.setVisible(true);
185     }
186 
removeListener()187     private void removeListener() {
188         if (keyListener != null) {
189             textArea.removeKeyListener(keyListener);
190             keyListener = null;
191         }
192     }
193 
194     @Override
dispose()195     public void dispose() {
196         if (null != frame) {
197             frame.dispose();
198             frame = null;
199         }
200     }
201 
generateFunctionKeyPress()202     public void generateFunctionKeyPress() {
203         try {
204             Robot robot = new Robot();
205             robot.waitForIdle();
206 
207             for (int i = 0; i < allKeyArr.length; i++) {
208                 keyPressedIndex = i;
209                 robot.keyPress(allKeyArr[i]);
210                 robot.keyRelease(allKeyArr[i]);
211                 robot.waitForIdle();
212             }
213             removeListener();
214 
215         } catch (AWTException e) {
216             throw new RuntimeException("Robot creation failed");
217         }
218     }
219 
main(String args[])220     public static void main(String args[]) {
221         AllKeyCode allKeyObj = new AllKeyCode();
222         allKeyObj.createAndShowGUI();
223         allKeyObj.generateFunctionKeyPress();
224         allKeyObj.dispose();
225 
226         System.out.println("Test Passed");
227     }
228 }
229