1
1
/*
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.
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
27
27
* @bug 8033936 8172510
28
28
* @summary Verify that correct ItemEvent is received while selection &
29
29
* deselection of multi select List items.
30
+ * @library /test/lib
31
+ * @build jdk.test.lib.Platform
32
+ * @run main ItemEventTest
30
33
*/
31
34
35
+ // Pass -save to the test to enable screenshots at each select/deselect
36
+
32
37
import java .awt .AWTException ;
33
38
import java .awt .Event ;
34
39
import java .awt .FlowLayout ;
37
42
import java .awt .Point ;
38
43
import java .awt .Rectangle ;
39
44
import java .awt .Robot ;
40
- import java .awt .event .KeyEvent ;
41
45
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 );
60
69
61
70
list = new List (4 , true );
62
71
list .add ("0" );
@@ -65,82 +74,109 @@ public ItemEventTest()
65
74
list .add ("3" );
66
75
67
76
add (list );
77
+
68
78
setSize (400 ,400 );
69
79
setLayout (new FlowLayout ());
70
80
pack ();
81
+ setLocationRelativeTo (null );
71
82
setVisible (true );
72
83
robot .waitForIdle ();
73
84
}
74
85
75
86
@ Override
87
+ @ SuppressWarnings ("deprecation" )
76
88
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 );
81
93
}
82
94
return true ;
83
95
}
84
96
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 () {
86
103
// When no ItemListener is added to List, parent's handleEvent is
87
104
// called with ItemEvent.
88
105
performTest ();
89
106
}
90
107
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 ()));
98
111
performTest ();
99
112
}
100
113
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 );
107
121
108
- String osName = System .getProperty ("os.name" );
109
- boolean isMac = osName .contains ("Mac" ) || osName .contains ("mac" );
110
- if (isMac ) {
122
+ if (Platform .isOSX ()) {
111
123
robot .keyPress (KeyEvent .VK_META );
112
- robot .waitForIdle ();
113
124
}
114
125
115
126
// First loop to select & Second loop to deselect the list items.
116
127
for (int j = 0 ; j < 2 ; ++j ) {
117
128
for (int i = 0 ; i < list .getItemCount (); ++i ) {
118
129
robot .mouseMove (loc .x , loc .y + i * dY );
130
+ robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
131
+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
119
132
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
+ }
124
137
}
125
138
}
126
139
127
- if ( isMac ) {
140
+ if ( Platform . isOSX () ) {
128
141
robot .keyRelease (KeyEvent .VK_META );
129
142
}
130
143
131
- if (!expectedSelectionOrder .equals (actualSelectionOrder .toString ())) {
132
- dispose ();
144
+ if (!expectedSelectionOrder .contentEquals (actualSelectionOrder )) {
145
+ saveImage (robot .createScreenCapture (rect ));
146
+
133
147
throw new RuntimeException ("ItemEvent for selection & deselection"
134
148
+ " of multi select List's item is not correct"
135
149
+ " Expected : " + expectedSelectionOrder
136
150
+ " Actual : " + actualSelectionOrder );
137
151
}
138
152
}
139
153
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
+ }
145
181
}
146
182
}
0 commit comments