|
1 | 1 | /*
|
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. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
21 | 21 | * questions.
|
22 | 22 | */
|
23 | 23 |
|
| 24 | +import java.awt.Dimension; |
| 25 | +import java.awt.Point; |
| 26 | +import java.awt.Robot; |
| 27 | +import java.awt.event.InputEvent; |
24 | 28 | import javax.swing.JButton;
|
25 | 29 | import javax.swing.JComboBox;
|
26 | 30 | import javax.swing.JFrame;
|
| 31 | +import javax.swing.SwingUtilities; |
27 | 32 |
|
28 | 33 | /*
|
29 | 34 | * @test
|
30 | 35 | * @bug 8322754
|
| 36 | + * @key headful |
31 | 37 | * @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 |
35 | 39 | */
|
36 | 40 |
|
37 | 41 | 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; |
48 | 47 |
|
49 | 48 | 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 | + } |
59 | 68 | }
|
60 | 69 |
|
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(); |
63 | 73 |
|
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 | + } |
69 | 79 |
|
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"); |
79 | 82 |
|
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"); |
83 | 88 |
|
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); |
85 | 100 | }
|
86 | 101 | }
|
0 commit comments