Skip to content

Commit d85560e

Browse files
lawrence-andrewaivanov-jdk
authored andcommittedAug 20, 2021
8267161: Write automated test case for JDK-4479161
Reviewed-by: serb, aivanov
1 parent 1ea437a commit d85560e

File tree

2 files changed

+122
-84
lines changed

2 files changed

+122
-84
lines changed
 

‎test/jdk/java/awt/im/4959409/bug4959409.html

-47
This file was deleted.
+122-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,52 +21,137 @@
2121
* questions.
2222
*/
2323

24-
/**
25-
*
24+
/*
25+
* @test
2626
* @bug 4959409
27-
* @author Naoto Sato
27+
* @summary Check whether pressing SHIFT + 1 triggers key event
28+
* @key headful
2829
*/
2930

30-
import java.awt.*;
31-
import java.awt.event.*;
32-
import javax.swing.*;
31+
import java.awt.Container;
32+
import java.awt.Robot;
33+
import java.awt.Rectangle;
34+
import java.awt.Point;
35+
import java.awt.BorderLayout;
36+
import java.awt.event.WindowAdapter;
37+
import java.awt.event.WindowEvent;
38+
import java.awt.event.KeyEvent;
39+
import java.awt.event.KeyAdapter;
40+
import java.awt.event.InputEvent;
3341

34-
public class bug4959409 extends javax.swing.JApplet {
35-
public void init() {
36-
new TestFrame();
37-
}
38-
}
42+
import java.util.concurrent.CountDownLatch;
43+
import java.util.concurrent.TimeUnit;
3944

40-
class TestFrame extends JFrame implements KeyListener {
41-
JTextField text;
42-
JLabel label;
43-
44-
TestFrame () {
45-
text = new JTextField();
46-
text.addKeyListener(this);
47-
label = new JLabel(" ");
48-
Container c = getContentPane();
49-
BorderLayout borderLayout1 = new BorderLayout();
50-
c.setLayout(borderLayout1);
51-
c.add(text, BorderLayout.CENTER);
52-
c.add(label, BorderLayout.SOUTH);
53-
setSize(300, 200);
54-
setVisible(true);
55-
}
45+
import javax.swing.JFrame;
46+
import javax.swing.JTextField;
47+
import javax.swing.JLabel;
48+
import javax.swing.SwingUtilities;
49+
50+
public class bug4959409 {
51+
52+
public final static int TIMEOUT = 20;
53+
public final static int DELAY = 300;
54+
private static JFrame frame;
55+
private static JTextField jTextField;
56+
private static JLabel jLabel;
57+
58+
public static void createUIAndTest() throws Exception {
59+
CountDownLatch frameVisibleLatch = new CountDownLatch(1);
60+
CountDownLatch keyPressedEventLatch = new CountDownLatch(1);
61+
final Point[] points = new Point[1];
62+
final Rectangle[] rect = new Rectangle[1];
63+
64+
SwingUtilities.invokeAndWait(() -> {
65+
frame = new JFrame("Test bug4959409");
66+
jTextField = new JTextField();
67+
jLabel = new JLabel();
68+
frame.setLayout(new BorderLayout());
69+
frame.addWindowListener(new WindowAdapter() {
70+
@Override
71+
public void windowOpened(WindowEvent e) {
72+
super.windowOpened(e);
73+
frameVisibleLatch.countDown();
74+
System.out.println("Frame opened");
75+
}
76+
});
77+
78+
jTextField.addKeyListener(new KeyAdapter() {
79+
@Override
80+
public void keyPressed(KeyEvent keyEvent) {
81+
super.keyPressed(keyEvent);
82+
int code = keyEvent.getKeyCode();
83+
int mod = keyEvent.getModifiersEx();
84+
if (code == '1' && mod == KeyEvent.SHIFT_DOWN_MASK) {
85+
keyPressedEventLatch.countDown();
86+
jLabel.setText("keyPressed received for Shift+1");
87+
System.out.println("keyPressed received for Shift+1");
88+
} else {
89+
jLabel.setText("Did not receive keyPressed for Shift+1");
90+
System.out.println("Did not receive keyPressed for Shift+1");
91+
}
92+
}
93+
});
5694

57-
public void keyPressed(KeyEvent e) {
58-
int code = e.getKeyCode();
59-
int mods = e.getModifiers();
60-
if (code == '1' && mods == KeyEvent.SHIFT_MASK) {
61-
label.setText("KEYPRESS received for Shift+1");
62-
} else {
63-
label.setText(" ");
95+
Container container = frame.getContentPane();
96+
container.add(jTextField, BorderLayout.SOUTH);
97+
container.add(jLabel, BorderLayout.CENTER);
98+
frame.setSize(300, 300);
99+
frame.setLocationRelativeTo(null);
100+
frame.setAlwaysOnTop(true);
101+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
102+
frame.setVisible(true);
103+
});
104+
105+
Robot robot = new Robot();
106+
robot.setAutoDelay(DELAY);
107+
robot.waitForIdle();
108+
109+
if (!frameVisibleLatch.await(TIMEOUT, TimeUnit.SECONDS)) {
110+
throw new RuntimeException("Frame is not visible after " + TIMEOUT + " seconds");
111+
}
112+
113+
SwingUtilities.invokeAndWait(() -> {
114+
points[0] = jTextField.getLocationOnScreen();
115+
rect[0] = jTextField.getBounds();
116+
});
117+
118+
clickTextField(robot, points[0].x + rect[0].width / 2,
119+
points[0].y + rect[0].height / 2);
120+
121+
// Press SHIFT + 1 keys
122+
robot.waitForIdle();
123+
robot.keyPress(KeyEvent.VK_SHIFT);
124+
robot.keyPress(KeyEvent.VK_1);
125+
robot.keyRelease(KeyEvent.VK_1);
126+
robot.keyRelease(KeyEvent.VK_SHIFT);
127+
robot.waitForIdle();
128+
129+
if (!keyPressedEventLatch.await(TIMEOUT, TimeUnit.SECONDS)) {
130+
throw new RuntimeException("Did not receive keyPressed for Shift + 1 , test failed");
64131
}
65132
}
66133

67-
public void keyTyped(KeyEvent e) {
134+
public static void clickTextField(final Robot robot, final int X, final int Y) {
135+
robot.delay(DELAY);
136+
robot.waitForIdle();
137+
robot.mouseMove(X, Y);
138+
robot.waitForIdle();
139+
140+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
141+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
142+
robot.waitForIdle();
68143
}
69144

70-
public void keyReleased(KeyEvent e) {
145+
public static void main(String[] args) throws Exception {
146+
try {
147+
createUIAndTest();
148+
} finally {
149+
SwingUtilities.invokeAndWait(() -> {
150+
if (frame != null) {
151+
frame.dispose();
152+
}
153+
});
154+
}
71155
}
72156
}
157+

0 commit comments

Comments
 (0)
Please sign in to comment.