Skip to content

Commit 605b53e

Browse files
committed
8348299: Update List/ItemEventTest/ItemEventTest.java
Use thread-safe StringBuffer to track selecting/deselecting items. Use auto waitForIdle for all events. Log handleEvent and ItemListener. Take screenshot of the list on failure; Optionally take screenshot after each mouse press+release. Reviewed-by: azvegint, prr, kizune
1 parent cba0f78 commit 605b53e

File tree

1 file changed

+90
-54
lines changed

1 file changed

+90
-54
lines changed
Lines changed: 90 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 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
@@ -27,8 +27,13 @@
2727
* @bug 8033936 8172510
2828
* @summary Verify that correct ItemEvent is received while selection &
2929
* deselection of multi select List items.
30+
* @library /test/lib
31+
* @build jdk.test.lib.Platform
32+
* @run main ItemEventTest
3033
*/
3134

35+
// Pass -save to the test to enable screenshots at each select/deselect
36+
3237
import java.awt.AWTException;
3338
import java.awt.Event;
3439
import java.awt.FlowLayout;
@@ -37,26 +42,30 @@
3742
import java.awt.Point;
3843
import java.awt.Rectangle;
3944
import java.awt.Robot;
40-
import java.awt.event.KeyEvent;
4145
import java.awt.event.InputEvent;
42-
import java.awt.event.ItemEvent;
43-
import java.awt.event.ItemListener;
44-
45-
public class ItemEventTest extends Frame
46-
{
47-
List list;
48-
final String expectedSelectionOrder;
49-
StringBuilder actualSelectionOrder;
50-
Robot robot;
51-
52-
public ItemEventTest()
53-
{
54-
try {
55-
robot = new Robot();
56-
} catch(AWTException e) {
57-
throw new RuntimeException(e.getMessage());
58-
}
59-
expectedSelectionOrder = "01230123";
46+
import java.awt.event.KeyEvent;
47+
import java.awt.image.RenderedImage;
48+
import java.io.File;
49+
import java.io.IOException;
50+
51+
import javax.imageio.ImageIO;
52+
53+
import jdk.test.lib.Platform;
54+
55+
public final class ItemEventTest extends Frame {
56+
private static final String expectedSelectionOrder = "01230123";
57+
58+
private static boolean saveScreenshots;
59+
60+
private final StringBuffer actualSelectionOrder
61+
= new StringBuffer(expectedSelectionOrder.length());
62+
63+
private final List list;
64+
private final Robot robot;
65+
66+
private ItemEventTest() throws AWTException {
67+
robot = new Robot();
68+
robot.setAutoWaitForIdle(true);
6069

6170
list = new List(4, true);
6271
list.add("0");
@@ -65,82 +74,109 @@ public ItemEventTest()
6574
list.add("3");
6675

6776
add(list);
77+
6878
setSize(400,400);
6979
setLayout(new FlowLayout());
7080
pack();
81+
setLocationRelativeTo(null);
7182
setVisible(true);
7283
robot.waitForIdle();
7384
}
7485

7586
@Override
87+
@SuppressWarnings("deprecation")
7688
public boolean handleEvent(Event e) {
77-
if (e.target instanceof List) {
78-
if (e.id == Event.LIST_DESELECT || e.id == Event.LIST_SELECT) {
79-
actualSelectionOrder.append(e.arg);
80-
}
89+
if ((e.target instanceof List)
90+
&& (e.id == Event.LIST_DESELECT
91+
|| e.id == Event.LIST_SELECT)) {
92+
logEvent("handleEvent: ", e.arg);
8193
}
8294
return true;
8395
}
8496

85-
void testHandleEvent() {
97+
private void logEvent(String method, Object listItem) {
98+
actualSelectionOrder.append(listItem);
99+
System.out.println(method + listItem);
100+
}
101+
102+
private void testHandleEvent() {
86103
// When no ItemListener is added to List, parent's handleEvent is
87104
// called with ItemEvent.
88105
performTest();
89106
}
90107

91-
void testItemListener() {
92-
list.addItemListener(new ItemListener() {
93-
@Override
94-
public void itemStateChanged(ItemEvent ie) {
95-
actualSelectionOrder.append(ie.getItem());
96-
}
97-
});
108+
private void testItemListener() {
109+
list.addItemListener(ie
110+
-> logEvent("testItemListener: ", ie.getItem()));
98111
performTest();
99112
}
100113

101-
void performTest() {
102-
actualSelectionOrder = new StringBuilder();
103-
Point loc = list.getLocationOnScreen();
104-
Rectangle rect = list.getBounds();
105-
int dY = rect.height / list.getItemCount();
106-
loc = new Point(loc.x + 10, loc.y + 5);
114+
private void performTest() {
115+
actualSelectionOrder.setLength(0);
116+
117+
final Rectangle rect = getListBoundsOnScreen();
118+
final int dY = rect.height / list.getItemCount();
119+
final Point loc = new Point(rect.x + rect.width / 2,
120+
rect.y + dY / 2);
107121

108-
String osName = System.getProperty("os.name");
109-
boolean isMac = osName.contains("Mac") || osName.contains("mac");
110-
if(isMac) {
122+
if (Platform.isOSX()) {
111123
robot.keyPress(KeyEvent.VK_META);
112-
robot.waitForIdle();
113124
}
114125

115126
// First loop to select & Second loop to deselect the list items.
116127
for (int j = 0; j < 2; ++j) {
117128
for (int i = 0; i < list.getItemCount(); ++i) {
118129
robot.mouseMove(loc.x, loc.y + i * dY);
130+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
131+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
119132
robot.waitForIdle();
120-
robot.mousePress(InputEvent.BUTTON1_MASK);
121-
robot.waitForIdle();
122-
robot.mouseRelease(InputEvent.BUTTON1_MASK);
123-
robot.waitForIdle();
133+
134+
if (saveScreenshots) {
135+
saveImage(robot.createScreenCapture(rect));
136+
}
124137
}
125138
}
126139

127-
if(isMac) {
140+
if (Platform.isOSX()) {
128141
robot.keyRelease(KeyEvent.VK_META);
129142
}
130143

131-
if (!expectedSelectionOrder.equals(actualSelectionOrder.toString())) {
132-
dispose();
144+
if (!expectedSelectionOrder.contentEquals(actualSelectionOrder)) {
145+
saveImage(robot.createScreenCapture(rect));
146+
133147
throw new RuntimeException("ItemEvent for selection & deselection"
134148
+ " of multi select List's item is not correct"
135149
+ " Expected : " + expectedSelectionOrder
136150
+ " Actual : " + actualSelectionOrder);
137151
}
138152
}
139153

140-
public static void main(String args[]) {
141-
ItemEventTest test = new ItemEventTest();
142-
test.testHandleEvent();
143-
test.testItemListener();
144-
test.dispose();
154+
private Rectangle getListBoundsOnScreen() {
155+
return new Rectangle(list.getLocationOnScreen(),
156+
list.getSize());
157+
}
158+
159+
private static int imageNo = 0;
160+
161+
private static void saveImage(RenderedImage image) {
162+
try {
163+
ImageIO.write(image,
164+
"png",
165+
new File(String.format("image-%02d.png",
166+
++imageNo)));
167+
} catch (IOException ignored) {
168+
}
169+
}
170+
171+
public static void main(String[] args) throws AWTException {
172+
saveScreenshots = args.length > 0 && "-save".equals(args[0]);
173+
174+
ItemEventTest test = new ItemEventTest();
175+
try {
176+
test.testHandleEvent();
177+
test.testItemListener();
178+
} finally {
179+
test.dispose();
180+
}
145181
}
146182
}

0 commit comments

Comments
 (0)