Skip to content

Commit e2a503e

Browse files
committed
8347277: java/awt/Focus/ComponentLostFocusTest.java fails intermittently
Reviewed-by: serb
1 parent c6c451a commit e2a503e

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

test/jdk/java/awt/Focus/ComponentLostFocusTest.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2004, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2004, 2025, 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
@@ -35,24 +35,31 @@
3535
import java.awt.Frame;
3636
import java.awt.KeyboardFocusManager;
3737
import java.awt.Point;
38+
import java.awt.Rectangle;
3839
import java.awt.Robot;
3940
import java.awt.TextField;
41+
import java.awt.Toolkit;
4042
import java.awt.event.FocusAdapter;
4143
import java.awt.event.FocusEvent;
4244
import java.awt.event.InputEvent;
4345
import java.awt.event.WindowAdapter;
4446
import java.awt.event.WindowEvent;
47+
import java.io.File;
48+
import java.util.concurrent.CountDownLatch;
49+
import java.util.concurrent.TimeUnit;
50+
51+
import javax.imageio.ImageIO;
4552

4653
public class ComponentLostFocusTest {
4754

4855
static Frame frame;
4956
static TextField tf;
5057
static Robot r;
5158
static Dialog dialog = null;
52-
static volatile boolean passed;
5359
static volatile Point loc;
5460
static volatile int width;
5561
static volatile int top;
62+
static final CountDownLatch focusGainedLatch = new CountDownLatch(1);
5663

5764
private static void createTestUI() {
5865

@@ -75,11 +82,7 @@ public void windowGainedFocus(WindowEvent e) {
7582

7683
public static void doTest() {
7784
System.out.println("dialog.setVisible.... ");
78-
new Thread(new Runnable() {
79-
public void run() {
80-
dialog.setVisible(true);
81-
}
82-
}).start();
85+
new Thread(() -> dialog.setVisible(true)).start();
8386

8487
// The bug is that this construction leads to the redundant xRequestFocus
8588
// By the way, the requestFocusInWindow() works fine before the fix
@@ -98,7 +101,7 @@ public void run() {
98101
tf.addFocusListener(new FocusAdapter() {
99102
public void focusGained(FocusEvent e) {
100103
System.out.println("TextField gained focus: " + e);
101-
passed = true;
104+
focusGainedLatch.countDown();
102105
}
103106
});
104107

@@ -116,6 +119,17 @@ private static void doRequestFocusToTextField() {
116119
tf.requestFocus();
117120
}
118121

122+
private static void captureScreen() {
123+
try {
124+
final Rectangle screenBounds = new Rectangle(
125+
Toolkit.getDefaultToolkit().getScreenSize());
126+
ImageIO.write(r.createScreenCapture(screenBounds),
127+
"png", new File("ComponentLostFocusTest.png"));
128+
} catch (Exception e) {
129+
e.printStackTrace();
130+
}
131+
}
132+
119133
public static final void main(String args[]) throws Exception {
120134
r = new Robot();
121135
r.setAutoDelay(100);
@@ -138,9 +152,10 @@ public static final void main(String args[]) throws Exception {
138152
System.out.println("Focus owner: " +
139153
KeyboardFocusManager.getCurrentKeyboardFocusManager().
140154
getFocusOwner());
141-
142-
if (!passed) {
143-
throw new RuntimeException("TextField got no focus! Test failed.");
155+
if (!focusGainedLatch.await(5, TimeUnit.SECONDS)) {
156+
captureScreen();
157+
throw new RuntimeException("Waited too long, " +
158+
"TextField got no focus! Test failed.");
144159
}
145160
} finally {
146161
EventQueue.invokeAndWait(() -> {
@@ -151,4 +166,3 @@ public static final void main(String args[]) throws Exception {
151166
}
152167
}
153168
}
154-

0 commit comments

Comments
 (0)