File: KeyEventLocationTest.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (212 lines) | stat: -rw-r--r-- 8,114 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @key headful
 * @bug 4424517
 * @summary Verify the mapping of various KeyEvents with their KeyLocations
 * is as expected.
 * @run main KeyEventLocationTest
 */

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;

import javax.swing.SwingUtilities;

public class KeyEventLocationTest {

    private static volatile Frame frame;
    private static volatile boolean keyPressed;
    private static volatile boolean keyReleased;
    private static volatile boolean keyTyped;
    private static volatile Robot robot;
    private static volatile int xLocation;
    private static volatile int yLocation;
    private static volatile int width;
    private static volatile int height;
    private static volatile Label label = new Label();
    private static volatile String currentString = "";

    private static int[] keyEvents = { KeyEvent.VK_0, KeyEvent.VK_1,
        KeyEvent.VK_2, KeyEvent.VK_3, KeyEvent.VK_4, KeyEvent.VK_5,
        KeyEvent.VK_6, KeyEvent.VK_7, KeyEvent.VK_8, KeyEvent.VK_9,
        KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D,
        KeyEvent.VK_E, KeyEvent.VK_F, KeyEvent.VK_G, KeyEvent.VK_H,
        KeyEvent.VK_I, KeyEvent.VK_J, KeyEvent.VK_K, KeyEvent.VK_L,
        KeyEvent.VK_M, KeyEvent.VK_N, KeyEvent.VK_O, KeyEvent.VK_P,
        KeyEvent.VK_Q, KeyEvent.VK_R, KeyEvent.VK_S, KeyEvent.VK_T,
        KeyEvent.VK_U, KeyEvent.VK_V, KeyEvent.VK_W, KeyEvent.VK_X,
        KeyEvent.VK_Y, KeyEvent.VK_Z, KeyEvent.VK_BACK_QUOTE,
        KeyEvent.VK_BACK_SLASH, KeyEvent.VK_BACK_SPACE,
        KeyEvent.VK_CLOSE_BRACKET, KeyEvent.VK_COMMA, KeyEvent.VK_EQUALS,
        KeyEvent.VK_ESCAPE, KeyEvent.VK_MINUS, KeyEvent.VK_OPEN_BRACKET,
        KeyEvent.VK_PERIOD, KeyEvent.VK_QUOTE, KeyEvent.VK_SEMICOLON,
        KeyEvent.VK_SLASH, KeyEvent.VK_SPACE };

    private static int specialKeyEvents[] = { KeyEvent.VK_F1, KeyEvent.VK_F2,
        KeyEvent.VK_F3, KeyEvent.VK_F4, KeyEvent.VK_F5, KeyEvent.VK_F6,
        KeyEvent.VK_F7, KeyEvent.VK_F8, KeyEvent.VK_F9, KeyEvent.VK_F10 };

    private static void createGUI() {
        frame = new Frame("Test frame");
        frame.setLayout(new BorderLayout());
        frame.setAlwaysOnTop(true);

        frame.addKeyListener(new KeyListener() {
            public void keyPressed(KeyEvent event) {
                try {
                    handleEvent("keyPressed", event);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                keyPressed = true;
            }

            public void keyReleased(KeyEvent event) {
                try {
                    handleEvent("keyReleased", event);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                keyReleased = true;
            }

            public void keyTyped(KeyEvent event) {
                try {
                    handleEvent("keyTyped", event);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                keyTyped = true;
            }

            private void handleEvent(String eventString, KeyEvent event)
                throws Exception {
                label.setText(eventString + " triggered for " + event);
                if ((event.getID() == KeyEvent.KEY_TYPED
                    && event.getKeyLocation() != KeyEvent.KEY_LOCATION_UNKNOWN)
                    || ((event.getID() == KeyEvent.KEY_PRESSED
                    || event.getID() == KeyEvent.KEY_PRESSED)
                    && event.getKeyLocation()
                    != KeyEvent.KEY_LOCATION_STANDARD)) {
                    throw new Exception("FAIL: Incorrect KeyLocation: "
                        + event.getKeyLocation() + " returned when "
                        + eventString + " triggered for " + event.getKeyChar());
                }
            }
        });
        label.setText("Current Event: ");
        frame.add(label, BorderLayout.SOUTH);
        frame.setSize(600, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.toFront();
    }

    private static void doTest() throws Exception {
        robot = new Robot();
        robot.setAutoWaitForIdle(true);
        robot.setAutoDelay(100);

        SwingUtilities.invokeAndWait(() -> {
            xLocation = frame.getLocationOnScreen().x;
            yLocation = frame.getLocationOnScreen().y;
            width = frame.getWidth();
            height = frame.getHeight();
        });

        robot.mouseMove(xLocation + width / 2, yLocation + height / 2);
        robot.mousePress(MouseEvent.BUTTON1_MASK);
        robot.mouseRelease(MouseEvent.BUTTON1_MASK);

        for (int i = 0; i < keyEvents.length; i++) {
            resetValues();
            robot.keyPress(keyEvents[i]);
            robot.delay(200);
            if (!keyPressed) {
                throw new Exception(
                    "FAIL: keyPressed did not get triggered for "
                        + KeyEvent.getKeyText(keyEvents[i]));
            }
            robot.keyRelease(keyEvents[i]);
            robot.delay(200);
            if (!keyReleased) {
                throw new Exception(
                    "FAIL: keyReleased did not get triggered for "
                        + KeyEvent.getKeyText(keyEvents[i]));
            }
            robot.delay(200);
            if (!keyTyped) {
                throw new Exception("FAIL: keyTyped did not get triggered for "
                    + KeyEvent.getKeyText(keyEvents[i]));
            }
        }

        for (int i = 0; i < specialKeyEvents.length; i++) {
            resetValues();
            robot.keyPress(specialKeyEvents[i]);
            robot.delay(200);
            if (!keyPressed) {
                throw new Exception("FAIL: keyPressed did not get triggered"
                    + " for " + KeyEvent.getKeyText(specialKeyEvents[i]));
            }
            robot.keyRelease(specialKeyEvents[i]);
            robot.delay(200);
            if (!keyReleased) {
                throw new Exception("FAIL: keyReleased got triggered for "
                    + KeyEvent.getKeyText(specialKeyEvents[i]));
            }
            robot.delay(200);
            if (keyTyped) {
                throw new Exception("FAIL: keyTyped got triggered for "
                    + KeyEvent.getKeyText(specialKeyEvents[i]));
            }
        }
    }

    private static void resetValues() {
        keyPressed = false;
        keyReleased = false;
        keyTyped = false;
    }

    public static void main(String[] args) throws Exception {
        try {
            EventQueue.invokeAndWait(() -> createGUI());
            doTest();
            System.out.println("Test Passed");
        } finally {
            if (frame != null)
                EventQueue.invokeAndWait(() -> frame.dispose());
        }
    }
}