Skip to content

Commit d4d1835

Browse files
author
Alexander Zvegintsev
committed
8352860: Open source events tests batch0
Reviewed-by: psadhukhan
1 parent c9c3c15 commit d4d1835

File tree

2 files changed

+302
-0
lines changed

2 files changed

+302
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright (c) 2000, 2025, 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+
@bug 4087762
27+
@summary Sometimes click events are missing when you click the color components alternately.
28+
@key headful
29+
@library /test/jdk/java/awt/regtesthelpers
30+
@build Util
31+
@run main ClickEventsTest
32+
*/
33+
34+
import java.awt.Color;
35+
import java.awt.Component;
36+
import java.awt.Dimension;
37+
import java.awt.EventQueue;
38+
import java.awt.Frame;
39+
import java.awt.Graphics;
40+
import java.awt.Robot;
41+
import java.awt.event.MouseAdapter;
42+
import java.awt.event.MouseEvent;
43+
import java.util.concurrent.BrokenBarrierException;
44+
import java.util.concurrent.CyclicBarrier;
45+
import java.util.concurrent.TimeUnit;
46+
import java.util.concurrent.TimeoutException;
47+
48+
import test.java.awt.regtesthelpers.Util;
49+
50+
public class ClickEventsTest {
51+
static Frame frame;
52+
static ColorComponent redComponent;
53+
static ColorComponent blueComponent;
54+
static Robot robot;
55+
56+
public static void main(String[] args) throws Exception {
57+
try {
58+
EventQueue.invokeAndWait(ClickEventsTest::createAndShowGUI);
59+
test();
60+
} finally {
61+
EventQueue.invokeAndWait(() -> {
62+
if (frame != null) {
63+
frame.dispose();
64+
}
65+
});
66+
}
67+
}
68+
69+
private static void test() throws Exception {
70+
robot = new Robot();
71+
robot.waitForIdle();
72+
robot.delay(500);
73+
74+
for (int i = 0; i < 10; i++) {
75+
redComponent.clickAndCheck();
76+
blueComponent.clickAndCheck();
77+
}
78+
}
79+
80+
private static void createAndShowGUI() {
81+
frame = new Frame("ClickEventsTest");
82+
redComponent = new ColorComponent(Color.RED);
83+
blueComponent = new ColorComponent(Color.BLUE);
84+
85+
frame.add("North", redComponent);
86+
frame.add("South", blueComponent);
87+
88+
frame.pack();
89+
frame.setLocationRelativeTo(null);
90+
frame.setVisible(true);
91+
}
92+
93+
static class ColorComponent extends Component {
94+
public Color myColor;
95+
96+
private final CyclicBarrier barrier = new CyclicBarrier(2);
97+
98+
private final MouseAdapter mouseAdapter = new MouseAdapter() {
99+
public void mouseClicked(MouseEvent event) {
100+
System.out.println(myColor + " area clicked");
101+
try {
102+
barrier.await(1, TimeUnit.SECONDS);
103+
} catch (InterruptedException | BrokenBarrierException | TimeoutException e) {
104+
throw new RuntimeException(e);
105+
}
106+
}
107+
};
108+
109+
public ColorComponent(Color c) {
110+
myColor = c;
111+
addMouseListener(mouseAdapter);
112+
}
113+
114+
public Dimension getPreferredSize() {
115+
return new Dimension(200, 100);
116+
}
117+
118+
public void paint(Graphics g) {
119+
g.setColor(myColor);
120+
g.fillRect(0, 0, 200, 100);
121+
}
122+
123+
public void clickAndCheck() throws InterruptedException, BrokenBarrierException {
124+
barrier.reset();
125+
Util.clickOnComp(this, robot);
126+
try {
127+
barrier.await(1, TimeUnit.SECONDS);
128+
} catch (TimeoutException e) {
129+
throw new RuntimeException(myColor + " was not clicked");
130+
}
131+
}
132+
}
133+
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Copyright (c) 1999, 2025, 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+
* @bug 4219344
27+
* @summary tests that WINDOW_ACTIVATED events are generated properly
28+
* @key headful
29+
* @library /test/jdk/java/awt/regtesthelpers
30+
* @build Util
31+
* @run main WindowActivatedEventTest
32+
*/
33+
34+
import test.java.awt.regtesthelpers.Util;
35+
36+
import java.awt.Dialog;
37+
import java.awt.EventQueue;
38+
import java.awt.Frame;
39+
import java.awt.Robot;
40+
import java.awt.Window;
41+
import java.awt.event.WindowEvent;
42+
import java.awt.event.WindowAdapter;
43+
import java.lang.reflect.InvocationTargetException;
44+
import java.util.Arrays;
45+
import java.util.concurrent.atomic.AtomicInteger;
46+
47+
public class WindowActivatedEventTest {
48+
49+
static Robot robot;
50+
static Frame frame;
51+
static Dialog dialog;
52+
53+
public static void main(String[] args) throws Exception {
54+
robot = new Robot();
55+
56+
try {
57+
EventQueue.invokeAndWait(WindowActivatedEventTest::createAndShowGUI);
58+
robot.waitForIdle();
59+
robot.delay(500);
60+
61+
Util.clickOnComp(dialog, robot);
62+
63+
robot.waitForIdle();
64+
robot.delay(500);
65+
66+
for (int i = 0; i < 3 ; i++) {
67+
clickAndCheck(frame);
68+
clickAndCheck(dialog);
69+
}
70+
} finally {
71+
EventQueue.invokeAndWait(() -> {
72+
if (frame != null) {
73+
frame.dispose();
74+
}
75+
if (dialog != null) {
76+
dialog.dispose();
77+
}
78+
});
79+
}
80+
}
81+
82+
private static void clickAndCheck(Window windowToFocus)
83+
throws InterruptedException, InvocationTargetException {
84+
Window oppositeWindow = (windowToFocus == frame) ? dialog : frame;
85+
86+
System.out.println("Clicking on " + windowToFocus);
87+
88+
EventQueue.invokeAndWait(() -> {
89+
if (windowToFocus.isFocused() || !oppositeWindow.isFocused()) {
90+
throw new RuntimeException("%s isFocused %b, %s isFocused %b".formatted(
91+
windowToFocus.getName(), windowToFocus.isFocused(),
92+
oppositeWindow.getName(), oppositeWindow.isFocused()
93+
));
94+
}
95+
});
96+
97+
WindowEventLogger windowLogger = WindowEventLogger.getFromWindow(windowToFocus);
98+
WindowEventLogger oppositeWindowLogger = WindowEventLogger.getFromWindow(oppositeWindow);
99+
100+
windowLogger.resetCounters();
101+
oppositeWindowLogger.resetCounters();
102+
103+
Util.clickOnComp(windowToFocus, robot);
104+
105+
robot.delay(500);
106+
107+
int windowActivatedCount = windowLogger.activatedCount.get();
108+
int windowDeactivatedCount = windowLogger.deactivatedCount.get();
109+
int oppositeWindowActivatedCount = oppositeWindowLogger.activatedCount.get();
110+
int oppositeWindowDeactivatedCount = oppositeWindowLogger.deactivatedCount.get();
111+
112+
if (windowActivatedCount != 1
113+
|| windowDeactivatedCount != 0
114+
|| oppositeWindowActivatedCount != 0
115+
|| oppositeWindowDeactivatedCount != 1) {
116+
throw new RuntimeException(
117+
"Invalid activated/deactivated count: %s (%d/%d) / %s (%d/%d)"
118+
.formatted(
119+
windowToFocus.getName(),
120+
windowActivatedCount,
121+
windowDeactivatedCount,
122+
oppositeWindow.getName(),
123+
oppositeWindowActivatedCount,
124+
oppositeWindowDeactivatedCount
125+
));
126+
}
127+
}
128+
129+
private static void createAndShowGUI() {
130+
frame = new Frame("frame WindowActivatedEventTest");
131+
dialog = new Dialog(frame, "dialog WindowActivatedEventTest");
132+
133+
frame.addWindowListener(new WindowEventLogger());
134+
dialog.addWindowListener(new WindowEventLogger());
135+
136+
frame.setBounds(400, 0, 200, 200);
137+
frame.setVisible(true);
138+
139+
dialog.setBounds(400, 200, 200, 200);
140+
dialog.setVisible(true);
141+
}
142+
143+
private static class WindowEventLogger extends WindowAdapter {
144+
final AtomicInteger activatedCount = new AtomicInteger(0);
145+
final AtomicInteger deactivatedCount = new AtomicInteger(0);
146+
147+
public void windowActivated(WindowEvent e) {
148+
activatedCount.incrementAndGet();
149+
System.out.println(e);
150+
}
151+
152+
public void windowDeactivated(WindowEvent e) {
153+
deactivatedCount.incrementAndGet();
154+
System.out.println(e);
155+
}
156+
157+
public void resetCounters() {
158+
activatedCount.set(0);
159+
deactivatedCount.set(0);
160+
}
161+
162+
public static WindowEventLogger getFromWindow(Window window) {
163+
return (WindowEventLogger) Arrays
164+
.stream(window.getWindowListeners())
165+
.filter(listener -> listener instanceof WindowEventLogger)
166+
.findFirst().get();
167+
}
168+
}
169+
}

0 commit comments

Comments
 (0)