Skip to content

Commit 5fe6cf8

Browse files
committed
8332901: Select{Current,New}ItemTest.java for Choice don't open popup on macOS
Move SelectCurrentItemTest.java to java/awt/Choice/SelectItem/. Move SelectNewItemTest.java to java/awt/Choice/SelectItem/. Use latches to control test flow instead of delays. Encapsulate the common logic in SelectCurrentItemTest. Provide overridable checkXXX() methods to modify conditions. Provide an overridable method which defines where to click in the choice popup to select an item. Backport-of: ef96a7b014795f366af3a90ef8f474cfb621197f
1 parent 7faedc6 commit 5fe6cf8

File tree

5 files changed

+276
-317
lines changed

5 files changed

+276
-317
lines changed

test/jdk/ProblemList.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,6 @@ java/awt/KeyboardFocusmanager/ConsumeNextMnemonicKeyTypedTest/ConsumeNextMnemoni
509509
java/awt/Window/GetScreenLocation/GetScreenLocationTest.java 8225787 linux-x64
510510
java/awt/Dialog/MakeWindowAlwaysOnTop/MakeWindowAlwaysOnTop.java 8266243 macosx-aarch64
511511

512-
# This test fails on macOS 14
513-
java/awt/Choice/SelectNewItemTest/SelectNewItemTest.java 8324782 macosx-all
514-
515512
# Wayland related
516513

517514
java/awt/FullScreen/FullscreenWindowProps/FullscreenWindowProps.java 8280991 linux-x64

test/jdk/java/awt/Choice/SelectCurrentItemTest/SelectCurrentItemTest.java

Lines changed: 0 additions & 134 deletions
This file was deleted.
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*
2+
* Copyright (c) 2002, 2024, 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.AWTException;
25+
import java.awt.BorderLayout;
26+
import java.awt.Choice;
27+
import java.awt.EventQueue;
28+
import java.awt.Frame;
29+
import java.awt.Point;
30+
import java.awt.Rectangle;
31+
import java.awt.Robot;
32+
import java.awt.event.InputEvent;
33+
import java.awt.event.ItemEvent;
34+
import java.awt.event.ItemListener;
35+
import java.awt.event.MouseAdapter;
36+
import java.awt.event.MouseEvent;
37+
import java.awt.event.WindowAdapter;
38+
import java.awt.event.WindowEvent;
39+
import java.lang.reflect.InvocationTargetException;
40+
import java.util.concurrent.CountDownLatch;
41+
import java.util.concurrent.TimeUnit;
42+
import java.util.concurrent.atomic.AtomicInteger;
43+
import java.util.concurrent.atomic.AtomicReference;
44+
45+
/*
46+
* @test
47+
* @bug 4902933 8197810
48+
* @summary Test that selecting the current item does not send an ItemEvent
49+
* @key headful
50+
* @run main SelectCurrentItemTest
51+
*/
52+
public class SelectCurrentItemTest
53+
extends WindowAdapter
54+
implements ItemListener {
55+
private static Frame frame;
56+
private static Choice choice;
57+
58+
private final Robot robot;
59+
60+
private final CountDownLatch windowOpened = new CountDownLatch(1);
61+
private final CountDownLatch mouseClicked = new CountDownLatch(1);
62+
63+
protected final CountDownLatch itemStateChanged = new CountDownLatch(1);
64+
65+
protected SelectCurrentItemTest() throws AWTException {
66+
robot = new Robot();
67+
robot.setAutoDelay(250);
68+
}
69+
70+
private void createUI() {
71+
frame = new Frame(getClass().getName());
72+
frame.setLayout(new BorderLayout());
73+
74+
choice = new Choice();
75+
for (int i = 0; i < 10; i++) {
76+
choice.add("Choice Item " + i);
77+
}
78+
choice.addItemListener(this);
79+
choice.addMouseListener(new MouseAdapter() {
80+
@Override
81+
public void mouseClicked(MouseEvent e) {
82+
System.out.println("mouseClicked()");
83+
mouseClicked.countDown();
84+
}
85+
});
86+
87+
frame.add(choice, BorderLayout.CENTER);
88+
89+
frame.addWindowListener(this);
90+
91+
frame.setLocationRelativeTo(null);
92+
frame.setResizable(false);
93+
frame.pack();
94+
frame.setVisible(true);
95+
}
96+
97+
protected final void runTest()
98+
throws InterruptedException, InvocationTargetException {
99+
try {
100+
doTest();
101+
} finally {
102+
EventQueue.invokeAndWait(this::dispose);
103+
}
104+
}
105+
106+
private void doTest()
107+
throws InterruptedException, InvocationTargetException {
108+
EventQueue.invokeAndWait(this::createUI);
109+
110+
if (!windowOpened.await(2, TimeUnit.SECONDS)) {
111+
throw new RuntimeException("Frame is not open in time");
112+
}
113+
robot.waitForIdle();
114+
115+
final int initialIndex = getSelectedIndex();
116+
117+
final Rectangle choiceRect = getChoiceRect();
118+
119+
// Open the choice popup
120+
robot.mouseMove(choiceRect.x + choiceRect.width - 10,
121+
choiceRect.y + choiceRect.height / 2);
122+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
123+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
124+
125+
if (!mouseClicked.await(500, TimeUnit.MILLISECONDS)) {
126+
throw new RuntimeException("Mouse is not clicked in time");
127+
}
128+
robot.waitForIdle();
129+
130+
// Click an item in the choice popup
131+
final Point pt = getClickLocation(choiceRect);
132+
robot.mouseMove(pt.x, pt.y);
133+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
134+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
135+
136+
robot.waitForIdle();
137+
138+
checkItemStateChanged();
139+
140+
final int currentIndex = getSelectedIndex();
141+
System.out.println("initialIndex = " + initialIndex);
142+
System.out.println("currentIndex = " + currentIndex);
143+
checkSelectedIndex(initialIndex, currentIndex);
144+
}
145+
146+
protected void checkItemStateChanged() throws InterruptedException {
147+
if (itemStateChanged.await(500, TimeUnit.MILLISECONDS)) {
148+
throw new RuntimeException("ItemEvent is received but unexpected");
149+
}
150+
}
151+
152+
protected void checkSelectedIndex(final int initialIndex,
153+
final int currentIndex) {
154+
if (initialIndex != currentIndex) {
155+
throw new RuntimeException("Selected index in Choice should not change");
156+
}
157+
}
158+
159+
/**
160+
* {@return the location for clicking choice popup to select an item}
161+
* @param choiceRect the bounds of the Choice component
162+
*/
163+
protected Point getClickLocation(final Rectangle choiceRect) {
164+
// Click on the first item in the popup, it's the selected item
165+
return new Point(choiceRect.x + choiceRect.width / 2,
166+
choiceRect.y + choiceRect.height + 3);
167+
}
168+
169+
private int getSelectedIndex()
170+
throws InterruptedException, InvocationTargetException {
171+
AtomicInteger index = new AtomicInteger();
172+
EventQueue.invokeAndWait(() -> index.set(choice.getSelectedIndex()));
173+
return index.get();
174+
}
175+
176+
private Rectangle getChoiceRect()
177+
throws InterruptedException, InvocationTargetException {
178+
AtomicReference<Rectangle> rect = new AtomicReference<>();
179+
EventQueue.invokeAndWait(
180+
() -> rect.set(new Rectangle(choice.getLocationOnScreen(),
181+
choice.getSize())));
182+
return rect.get();
183+
}
184+
185+
public static void main(String... args) throws Exception {
186+
new SelectCurrentItemTest().runTest();
187+
}
188+
189+
private void dispose() {
190+
if (frame != null) {
191+
frame.dispose();
192+
}
193+
}
194+
195+
@Override
196+
public final void itemStateChanged(ItemEvent e) {
197+
System.out.println("itemStateChanged: " + e);
198+
itemStateChanged.countDown();
199+
}
200+
201+
@Override
202+
public final void windowOpened(WindowEvent e) {
203+
System.out.println("windowActivated()");
204+
windowOpened.countDown();
205+
}
206+
207+
}

0 commit comments

Comments
 (0)