Skip to content

Commit a9cf1e3

Browse files
Victor Rudometovshipilev
authored andcommitted
8296632: Write a test to verify the content change of TextArea sends TextEvent
Backport-of: 59a308b9d0546471566b11f62ef9bdc169ca0b95
1 parent 8f13ec5 commit a9cf1e3

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright (c) 2007, 2022, 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+
import java.awt.Dimension;
25+
import java.awt.EventQueue;
26+
import java.awt.FlowLayout;
27+
import java.awt.Frame;
28+
import java.awt.Point;
29+
import java.awt.Robot;
30+
import java.awt.TextArea;
31+
import java.awt.event.InputEvent;
32+
import java.awt.event.KeyEvent;
33+
34+
/*
35+
* @test
36+
* @key headful
37+
* @bug 8296632
38+
* @summary Verify the content changes of a TextArea via TextListener.
39+
* @run main TextAreaTextEventTest
40+
*/
41+
public class TextAreaTextEventTest {
42+
43+
private static Frame frame;
44+
private volatile static TextArea textArea;
45+
private volatile static boolean textChanged = false;
46+
private volatile static Point textAreaAt;
47+
private volatile static Dimension textAreaSize;
48+
private static Robot robot = null;
49+
50+
public static void main(String[] args) throws Exception {
51+
try {
52+
EventQueue.invokeAndWait(TextAreaTextEventTest::initializeGUI);
53+
54+
robot = new Robot();
55+
robot.setAutoDelay(100);
56+
57+
robot.waitForIdle();
58+
EventQueue.invokeAndWait(() -> {
59+
textAreaAt = textArea.getLocationOnScreen();
60+
textAreaSize = textArea.getSize();
61+
});
62+
robot.mouseMove(textAreaAt.x + textAreaSize.width / 2,
63+
textAreaAt.y + textAreaSize.height / 2);
64+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
65+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
66+
typeKey(KeyEvent.VK_T);
67+
68+
robot.waitForIdle();
69+
if (!textChanged) {
70+
throw new RuntimeException(
71+
"FAIL: TextEvent not triggered when key 'T' typed on TextArea");
72+
}
73+
74+
typeKey(KeyEvent.VK_E);
75+
typeKey(KeyEvent.VK_S);
76+
typeKey(KeyEvent.VK_T);
77+
78+
textChanged = false;
79+
typeKey(KeyEvent.VK_ENTER);
80+
81+
robot.waitForIdle();
82+
if (!textChanged) {
83+
throw new RuntimeException(
84+
"FAIL: TextEvent not triggered when Enter pressed on TextArea");
85+
}
86+
87+
textChanged = false;
88+
robot.mouseMove(textAreaAt.x + 4, textAreaAt.y + 10);
89+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
90+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
91+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
92+
for (int i = 0; i < textAreaSize.width / 2; i++) {
93+
robot.mouseMove(textAreaAt.x + 4 + i, textAreaAt.y + 10);
94+
}
95+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
96+
97+
robot.waitForIdle();
98+
if (textChanged) {
99+
throw new RuntimeException(
100+
"FAIL: TextEvent triggered when text is selected on TextArea!");
101+
}
102+
103+
textChanged = false;
104+
typeKey(KeyEvent.VK_F3);
105+
106+
robot.waitForIdle();
107+
if (textChanged) {
108+
throw new RuntimeException(
109+
"FAIL: TextEvent triggered when special key F3 is pressed on TextArea!");
110+
}
111+
System.out.println("Test passed!");
112+
} finally {
113+
EventQueue.invokeAndWait(TextAreaTextEventTest::disposeFrame);
114+
}
115+
}
116+
117+
private static void initializeGUI() {
118+
frame = new Frame("Test Frame");
119+
frame.setLayout(new FlowLayout());
120+
textArea = new TextArea(5, 15);
121+
textArea.addTextListener((event) -> {
122+
System.out.println("Got a text event: " + event);
123+
textChanged = true;
124+
});
125+
frame.add(textArea);
126+
frame.pack();
127+
frame.setLocationRelativeTo(null);
128+
frame.setVisible(true);
129+
}
130+
131+
public static void disposeFrame() {
132+
if (frame != null) {
133+
frame.dispose();
134+
}
135+
}
136+
137+
private static void typeKey(int key) throws Exception {
138+
robot.keyPress(key);
139+
robot.keyRelease(key);
140+
}
141+
}

0 commit comments

Comments
 (0)