Skip to content

Commit 223eec5

Browse files
anass-bayaSendaoYan
authored andcommitted
8354219: Automate javax/swing/JComboBox/ComboPopupBug.java
Reviewed-by: aivanov, achung
1 parent 52a5583 commit 223eec5

File tree

1 file changed

+58
-43
lines changed

1 file changed

+58
-43
lines changed
Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 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
@@ -21,66 +21,81 @@
2121
* questions.
2222
*/
2323

24+
import java.awt.Dimension;
25+
import java.awt.Point;
26+
import java.awt.Robot;
27+
import java.awt.event.InputEvent;
2428
import javax.swing.JButton;
2529
import javax.swing.JComboBox;
2630
import javax.swing.JFrame;
31+
import javax.swing.SwingUtilities;
2732

2833
/*
2934
* @test
3035
* @bug 8322754
36+
* @key headful
3137
* @summary Verifies clicking JComboBox during frame closure causes Exception
32-
* @library /java/awt/regtesthelpers
33-
* @build PassFailJFrame
34-
* @run main/manual ComboPopupBug
38+
* @run main ComboPopupBug
3539
*/
3640

3741
public class ComboPopupBug {
38-
private static final String instructionsText = """
39-
This test is used to verify that clicking on JComboBox
40-
when frame containing it is about to close should not
41-
cause IllegalStateException.
42-
43-
A JComboBox is shown with Close button at the bottom.
44-
Click on Close and then click on JComboBox arrow button
45-
to try to show combobox popup.
46-
If IllegalStateException is thrown, test will automatically Fail
47-
otherwise click Pass.""";
42+
private static JFrame frame;
43+
private static JButton closeButton;
44+
private static JComboBox<String> comboBox;
45+
private static Robot robot;
46+
private static final int PADDING = 10;
4847

4948
public static void main(String[] args) throws Exception {
50-
PassFailJFrame.builder()
51-
.title("ComboPopup Instructions")
52-
.instructions(instructionsText)
53-
.testTimeOut(5)
54-
.rows(10)
55-
.columns(35)
56-
.testUI(ComboPopupBug::createUI)
57-
.build()
58-
.awaitAndCheck();
49+
try {
50+
robot = new Robot();
51+
robot.setAutoDelay(50);
52+
53+
SwingUtilities.invokeAndWait(ComboPopupBug::createUI);
54+
55+
robot.waitForIdle();
56+
robot.delay(1000);
57+
58+
SwingUtilities.invokeAndWait(() -> closeButton.doClick());
59+
60+
robot.waitForIdle();
61+
} finally {
62+
SwingUtilities.invokeAndWait(() -> {
63+
if (frame != null) {
64+
frame.dispose();
65+
}
66+
});
67+
}
5968
}
6069

61-
private static JFrame createUI() {
62-
JFrame frame = new JFrame("ComboPopup");
70+
private static void clickComboBox() {
71+
Point comboBoxLocation = comboBox.getLocationOnScreen();
72+
Dimension comboBoxSize = comboBox.getSize();
6373

64-
JComboBox<String> cb = new JComboBox<>();
65-
cb.setEditable(true);
66-
cb.addItem("test");
67-
cb.addItem("test2");
68-
cb.addItem("test3");
74+
robot.mouseMove(comboBoxLocation.x + comboBoxSize.width - PADDING,
75+
comboBoxLocation.y + comboBoxSize.height / 2);
76+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
77+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
78+
}
6979

70-
JButton b = new JButton("Close");
71-
b.addActionListener(
72-
(e)->{
73-
try {
74-
Thread.sleep(3000);
75-
} catch (Exception ignored) {
76-
}
77-
frame.setVisible(false);
78-
});
80+
private static void createUI() {
81+
frame = new JFrame("ComboPopup");
7982

80-
frame.getContentPane().add(cb, "North");
81-
frame.getContentPane().add(b, "South");
82-
frame.setSize(200, 200);
83+
comboBox = new JComboBox<>();
84+
comboBox.setEditable(true);
85+
comboBox.addItem("test");
86+
comboBox.addItem("test2");
87+
comboBox.addItem("test3");
8388

84-
return frame;
89+
closeButton = new JButton("Close");
90+
closeButton.addActionListener((e) -> {
91+
clickComboBox();
92+
frame.setVisible(false);
93+
});
94+
95+
frame.getContentPane().add(comboBox, "North");
96+
frame.getContentPane().add(closeButton, "South");
97+
frame.setSize(200, 200);
98+
frame.setLocationRelativeTo(null);
99+
frame.setVisible(true);
85100
}
86101
}

0 commit comments

Comments
 (0)