11/*
2- * Copyright (c) 2011, 2016 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2011, 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
2222 */
2323
2424/*
25- @test
26- @key headful
27- @bug 7050935
28- @summary closed/java/awt/Choice/WheelEventsConsumed/WheelEventsConsumed.html fails on win32
29- @library ../../regtesthelpers
30- @author Oleg Pekhovskiy: area=awt-choice
31- @build Util
32- @run main ChoiceMouseWheelTest
33- */
25+ * @test
26+ * @key headful
27+ * @bug 7050935
28+ * @summary closed/java/awt/Choice/WheelEventsConsumed/WheelEventsConsumed.html fails on win32
29+ * @library /java/awt/regtesthelpers
30+ * @build Util
31+ * @run main ChoiceMouseWheelTest
32+ */
3433
3534import test .java .awt .regtesthelpers .Util ;
3635
37- import java .awt .*;
38- import java .awt .event .*;
36+ import java .awt .Choice ;
37+ import java .awt .Dimension ;
38+ import java .awt .EventQueue ;
39+ import java .awt .FlowLayout ;
40+ import java .awt .Frame ;
41+ import java .awt .Point ;
42+ import java .awt .Rectangle ;
43+ import java .awt .Robot ;
44+ import java .awt .Toolkit ;
45+ import java .awt .event .InputEvent ;
46+ import java .awt .event .MouseAdapter ;
47+ import java .awt .event .MouseEvent ;
48+ import java .awt .event .WindowAdapter ;
49+ import java .awt .event .WindowEvent ;
3950
4051public class ChoiceMouseWheelTest extends Frame {
4152
4253 private volatile boolean itemChanged = false ;
4354 private volatile boolean wheelMoved = false ;
4455 private volatile boolean frameExited = false ;
56+ private final Choice choice ;
4557
46- public static void main (String [] args ) {
47- new ChoiceMouseWheelTest ();
58+ public static void main (String [] args ) throws Exception {
59+ ChoiceMouseWheelTest test = Util .invokeOnEDT (ChoiceMouseWheelTest ::new );
60+ try {
61+ test .test ();
62+ } finally {
63+ EventQueue .invokeAndWait (test ::dispose );
64+ }
4865 }
4966
5067 ChoiceMouseWheelTest () {
5168 super ("ChoiceMouseWheelTest" );
5269 setLayout (new FlowLayout ());
5370
54- Choice choice = new Choice ();
55-
5671 addWindowListener (new WindowAdapter () {
5772 @ Override
5873 public void windowClosing (WindowEvent e ) {
59- System . exit ( 0 );
74+ e . getWindow (). dispose ( );
6075 }
6176 });
6277
63- for (Integer i = 0 ; i < 50 ; i ++) {
64- choice .add (i .toString ());
78+ choice = new Choice ();
79+ for (int i = 0 ; i < 50 ; i ++) {
80+ choice .add (Integer .toString (i ));
6581 }
6682
67- choice .addItemListener (new ItemListener () {
68- public void itemStateChanged (ItemEvent e ) {
69- itemChanged = true ;
70- }
71- });
72- choice .addMouseWheelListener (new MouseWheelListener () {
73- public void mouseWheelMoved (MouseWheelEvent e ) {
74- wheelMoved = true ;
75- }
76- });
83+ choice .addItemListener (e -> itemChanged = true );
84+ choice .addMouseWheelListener (e -> wheelMoved = true );
7785
7886 addMouseListener (new MouseAdapter () {
7987 @ Override
@@ -84,71 +92,79 @@ public void mouseExited(MouseEvent e) {
8492
8593 add (choice );
8694 setSize (200 , 300 );
95+ setLocationRelativeTo (null );
8796 setVisible (true );
8897 toFront ();
98+ }
8999
90- try {
91- Robot robot = new Robot ();
92- robot .setAutoDelay (20 );
93- Util .waitForIdle (robot );
100+ private void test () throws Exception {
101+ Robot robot = new Robot ();
102+ robot .setAutoDelay (20 );
103+ robot .waitForIdle ();
104+ robot .delay (500 );
94105
106+ Rectangle choiceBounds = Util .invokeOnEDT (() -> {
95107 Point pt = choice .getLocationOnScreen ();
96108 Dimension size = choice .getSize ();
97- int x = pt .x + size .width / 3 ;
98- robot .mouseMove (x , pt .y + size .height / 2 );
109+ return new Rectangle (pt , size );
110+ });
111+
112+ int x = choiceBounds .x + choiceBounds .width / 3 ;
113+ robot .mouseMove (x , choiceBounds .y + choiceBounds .height / 2 );
99114
100- // Test mouse wheel over the choice
101- String name = Toolkit .getDefaultToolkit ().getClass ().getName ();
115+ // Test mouse wheel over the choice
116+ String name = Toolkit .getDefaultToolkit ().getClass ().getName ();
117+ boolean isXtoolkit = name .equals ("sun.awt.X11.XToolkit" );
118+ boolean isLWCToolkit = name .equals ("sun.lwawt.macosx.LWCToolkit" );
102119
103- // mouse wheel doesn't work for the choice on X11 and Mac, so skip it
104- if (!name .equals ("sun.awt.X11.XToolkit" )
105- && !name .equals ("sun.lwawt.macosx.LWCToolkit" )) {
106- robot .mouseWheel (1 );
107- Util .waitForIdle (robot );
120+ // mouse wheel doesn't work for the choice on X11 and Mac, so skip it
121+ if (!isXtoolkit && !isLWCToolkit ) {
122+ robot .mouseWheel (1 );
123+ robot .waitForIdle ();
108124
109- if (!wheelMoved || !itemChanged ) {
110- throw new RuntimeException ("Mouse Wheel over the choice failed!" );
111- }
125+ if (!wheelMoved || !itemChanged ) {
126+ throw new RuntimeException ("Mouse Wheel over the choice failed!" );
112127 }
128+ }
113129
114- // Test mouse wheel over the drop-down list
115- robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
116- Util .waitForIdle (robot );
117- robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
118- Util .waitForIdle (robot );
130+ // Test mouse wheel over the drop-down list
131+ robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
132+ robot .waitForIdle ();
133+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
134+ robot .waitForIdle ();
119135
120- int y = getLocationOnScreen ().y + getSize ().height ;
121- while (!frameExited && y >= 0 ) { // move to the bottom of drop-down list
122- robot .mouseMove (x , --y );
123- Util .waitForIdle (robot );
124- }
136+ frameExited = false ;
125137
126- if (x < 0 ) {
127- throw new RuntimeException ("Could not enter drop-down list!" );
128- }
138+ Rectangle frameBounds = Util .invokeOnEDT (this ::getBounds );
129139
130- y -= choice .getHeight () / 2 ;
131- robot .mouseMove (x , y ); // move to the last visible item in the drop-down list
132- Util .waitForIdle (robot );
140+ int y = frameBounds .y + frameBounds .height ;
141+ while (!frameExited && y >= frameBounds .y ) { // move to the bottom of drop-down list
142+ robot .mouseMove (x , --y );
143+ robot .waitForIdle ();
144+ }
133145
134- robot .mouseWheel (choice .getItemCount ()); // wheel to the last item
135- Util .waitForIdle (robot );
146+ if (y < frameBounds .y ) {
147+ throw new RuntimeException ("Could not enter drop-down list!" );
148+ }
136149
137- // click the last item
138- itemChanged = false ;
139- robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
140- Util .waitForIdle (robot );
141- robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
142- Util .waitForIdle (robot );
150+ y -= choiceBounds .height / 2 ;
151+ robot .mouseMove (x , y ); // move to the last visible item in the drop-down list
152+ robot .waitForIdle ();
143153
144- if (!itemChanged || choice .getSelectedIndex () != choice .getItemCount () - 1 ) {
145- throw new RuntimeException ("Mouse Wheel scroll position error!" );
146- }
154+ int scrollDirection = isLWCToolkit ? -1 : 1 ;
155+ // wheel to the last item
156+ robot .mouseWheel (scrollDirection * choice .getItemCount ());
157+ robot .waitForIdle ();
147158
148- dispose ();
149- } catch (AWTException e ) {
150- throw new RuntimeException ("AWTException occurred - problem creating robot!" );
159+ // click the last item
160+ itemChanged = false ;
161+ robot .mousePress (InputEvent .BUTTON1_DOWN_MASK );
162+ robot .waitForIdle ();
163+ robot .mouseRelease (InputEvent .BUTTON1_DOWN_MASK );
164+ robot .waitForIdle ();
165+
166+ if (!itemChanged || choice .getSelectedIndex () != choice .getItemCount () - 1 ) {
167+ throw new RuntimeException ("Mouse Wheel scroll position error!" );
151168 }
152169 }
153170}
154-
0 commit comments